-
4 Feb 2013 6:48 AM #1
My simple data example with associations does not work. Why not?
My simple data example with associations does not work. Why not?
From my point of view I build the simplest way of model with associations in ExtJS.
Model: Post --hasOne--> User
What I did:- Using a memory proxy
- Followed the rule: Proxy in Model
- Load a post object by Post.load(...).
(Here the full source: http://jsfiddle.net/7XRw4/4/)
PHP Code:Ext.define('Post', {
extend: 'Ext.data.Model',
fields: ['user_id', 'content'],
hasOne: 'User',
proxy: {
type: 'memory',
data: {
posts: [
{id:1, user_id:1, content:'foo'},
{id:2, user_id:1, content:'bar'}
]
},
reader: {
type: 'json',
root: 'posts'
}
}
});
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['name'],
proxy: {
type: 'memory',
data: {
users: [
{id:1, name:'hans'},
{id:2, name:'klaus'}
]
},
reader: {
type: 'json',
root: 'users'
}
}
});
Ext.onReady(function() {
console.log("Ext.onReady() invoked...");
Post.load('1', {
success: function(record, operation) {
thePost = record;
console.log('thePost:', thePost);
theUser = thePost.getUser();
console.log('theUser:', theUser);
alert('The user name: ' + theUser.get('name'));
// The user object will not be right loaded! Why?
}
});
});
-
4 Feb 2013 8:17 AM #2
Hi Marco,
You can simply put hasOne config under association config like-
For more info see - http://docs.sencha.com/ext-js/4-1/#!...ciation.HasOneCode:Ext.define('Post', { extend: 'Ext.data.Model', fields: ['user_id', 'content'], associations: [{ type: 'hasOne', model: 'User' }], proxy: { type: 'memory', data: { posts: [ {id:1, user_id:1, content:'foo'}, {id:2, user_id:1, content:'bar'} ] }, reader: { type: 'json', root: 'posts' } } });
sword-it.com, Sencha Developer House in Turkey - Istanbul University Technopark Suite 204.
-
4 Feb 2013 1:09 PM #3
Hi sword-it, thanks for the tip.
But it does not solve my problem.
Another hint?
Regards,
Marco
-
10 Feb 2013 11:41 AM #4
Hey guys, that is all? Can not believe.
Marco
-
10 Feb 2013 11:43 AM #5
-
12 Feb 2013 3:01 AM #6
Hi Marco,
When doing "getUser", this is asynchronous so requires a callback function to get at the user instance. For example (from the documentation here: http://docs.sencha.com/ext-js/4-1/#!/guide/data):
Hope that helps.Code:post.getUser(function(user) { console.log('Just got the user reference from the post: ' + user.get('name')) });Daniel Gallo
Sales Engineer EMEA
Sencha UK Ltd


Reply With Quote