PDA

View Full Version : [OPEN]Association between namespaced models doesn't generate getter function



icflorescu
28 Feb 2011, 8:34 AM
Suppose you have these related models:


Ext.regModel('User', {
fields: [ 'id', 'name', 'email' ],
associations: [
{ type: 'hasMany', model: 'Comment', name: 'comments' }
]
});

Ext.regModel('Comment', {
fields: [ 'id', 'message' ],
belongsTo: 'User'
});


In each Comment instance you'll get an automatically generated getUser(function(user, operation)) method.

However, if you define your models with namespaces like this:


Ext.regModel('App.models.User', {
fields: [ 'id', 'name', 'email' ],
associations: [
{ type: 'hasMany', model: 'App.models.Comment', name: 'comments' }
]
});

Ext.regModel('App.models.Comment', {
fields: [ 'id', 'message' ],
belongsTo: 'App.models.User'
});


...no getUser() method will be generated.

Namespacing your entire code (including model definitions) is generally a good practice as it helps you avoid general scope pollution.

Let me know if you need the entire test case for this.

TommyMaintz
28 Feb 2011, 4:39 PM
Thanks for reporting this. We will refactor this a little bit to support namespaced model associations in the next release.

wiznia
18 Aug 2011, 2:09 AM
I see that namespaced models now work, but if you specify all the configs by hand, like so:


Ext.regModel('App.models.User', {
fields: [ 'id', 'name', 'email' ],
associations: [
{ type: 'hasMany', model: 'App.models.Comment', name: 'comments' }
]
});

Ext.regModel('App.models.Comment', {
fields: [ 'id', 'message', 'user_id' ],
associations: [
{ type: 'belongsTo', model: 'App.models.User', getterName: "getUser", setterName: "setUser", foreignKey: "user_id" }
]
});


I think the association should work automatically by only using the last part of the model name "user" to generate the getters, setters and foreignkey.

hakana
5 Dec 2011, 4:25 AM
belongsto : { model : 'XXX.YYY.ZZZ', name : 'NNNN' }

If name attribute is specified it should be used to generate the setter getNNNN and setter setNNNN. I would also be nice if the setter and getter creation could be disabled, if only used with the reader and access the instance directly.

hakana
5 Dec 2011, 4:27 AM
It would be nice if the name of BelongsTo changes/duplicates to something like hasOne. We using this when a model refers to only one child model to avoid an extra store and then belongsTo is misleading.