PDA

View Full Version : Variables in a model : fields or ???



JFFortierQc
5 Dec 2011, 8:03 PM
Hi
When we create a new Model, we can set fields that can be mapped to a JSon object. So this said, if we want to set some variables to our model that are used for anything else, can we use fields?

For example :


Ext.regModel('User', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'phone', type: 'string', mapping: 'phoneNumber'}
]
});

All those fields can be map to a JSon file. If I want to add a new variable that will hold another Model that need to be instantiated once only, not a Store, only a single var, can we do something like :

Ext.regModel('User', {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'phone', type: 'string', mapping: 'phoneNumber'},

{name: 'myPersonalVar', type: 'MyModelType'},
]
});
Or what is the best practice. Actually even if I set it as MyModelType, it does not map to the JSon data. I could use a hasMany (Store), but for only one instance I think that it's too much. So, for now I set my var in the fields and its value to 'auto'. Then I create a method that I call to set the Model.

Something like that:


setMyModel: function(myObj) {
this.myPersonalVar = Ext.ModelMgr.create(myObj, 'MyModelType');
}


So, to resume my questions,
1- does fields may be use to hold custom var like other Model?
2- if not, how to set custom vars?
3- is there a better way to create child Model as a single instance, not hasMany?

Thanks for your help on that.
Always looking for best practice.

mitchellsimoens
6 Dec 2011, 7:23 AM
You cannot do that. If you wanted to have another model instance as a field, you would have to use the default type 'auto' on the field and then after the model instances are created, use the set method on the one model and pass in the other model instance.

JFFortierQc
6 Dec 2011, 9:00 AM
OK
So the fields is the way to manage variables on the Model. We did use 'auto', but wasn't sure if it was the best. About the variable, we are using this.variableName and then assign it a Model. To do so, we do it aver a function after the main model creation, not using the set. But both the set or the function dies the same I think.

Ho does the set works. I mean, I think in JavaScript the set and get does not really exist. And in real life those are properties that use function to control the set content. How does it works with Sencha. Can I create my own set for a specific properties?

JFFortierQc
6 Dec 2011, 9:53 PM
Done
Set is simple, and its a method. Thanks