PDA

View Full Version : Load model record. Why is this a bad practice



stewardsencha
6 Nov 2013, 6:00 AM
I guess I had trouble finding something that would work for me. So when I did, I noted in the docs.

Today I notice it has been voted -3 and marked by Sencha as BAD PRACTICE.




my.model.User.load(1,{
success: function(record, operation) {
My.DudeOnline = new my.model.User(record.data)
}
})


So what's the deal please? What makes this bad?

burnnat
6 Nov 2013, 9:25 AM
I'm not one of the downvoters, so I can't speak definitively, but the thing that sticks out to me is that you access the record data directly (the "data" property is undocumented, and not part of the public API) and then use that to create an entirely new record when the old would have sufficed.

So, rather than using record.data, you should really use the public getData() (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Model-method-getData) method (good):

my.model.User.load(1, {
success: function(record, operation) {
My.DudeOnline = new my.model.User(record.getData());
}
});

But, if all you're doing is creating a new copy of the record, you should just use the copy() (http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Model-method-copy) method (better):

my.model.User.load(1, {
success: function(record, operation) {
My.DudeOnline = record.copy();
}
});

But really, why do you need to create a new record at all? Just use the one that's already been created for you (best):

my.model.User.load(1, {
success: function(record, operation) {
My.DudeOnline = record;
}
});