PDA

View Full Version : Lazy Loading with hasMany association



lenoxDoe
15 Apr 2013, 3:28 PM
Hello, I'm trying to implementing Lazy Loading like in this example: http://dev.sencha.com/deploy/ext-4.0.0/examples/data/associations.html using


Ext.define('Post', { extend: 'Ext.data.Model', fields: ['title'], proxy: {type: 'jsonp', url: 'http://example.com/post.json'}});
Ext.define('User', { extend: 'Ext.data.Model', fields: ['name'], hasMany: 'Post', proxy: { type: 'jsonp', url : 'http://example.com/user.json'} });



User.load(1, {
success: function(user) {
var test = user.posts();
test.on('load', function(me) {
console.log(me.getCount());
});
test.load();
}
});
})



Ext.data.JsonP.callback1([{"name":"blalala"}]);
Ext.data.JsonP.callback2([{"title":"dfgdfgdgd"}]);


But me.getCount() is always 0. Any advice would be great! :-?

girish.arjunagi
15 Apr 2013, 11:42 PM
You should get the count on callback of the test load call.


test.load({
scope: this,
callback: function(records, operation, success) {
console.log(records.getCount());
}
})

lenoxDoe
16 Apr 2013, 1:15 AM
Hi girish.arjunagi,

thx for your post. It seems Record does not to have a getCount method and Record.data stores only the last object and not an array :(

girish.arjunagi
16 Apr 2013, 2:14 AM
lenoxDoe,

Please try getting the count on records[0] object.
console.log(records.getCount());

It will be helpful if you post the json data.

Thanks,

lenoxDoe
16 Apr 2013, 3:01 AM
Hi girish.arjunagi,

the json data is the same as posted in my first post:

user.json


Ext.data.JsonP.callback1([{"name":"blalala"}]);


post.json


Ext.data.JsonP.callback2([{"title":"dfgdfgdgd"}]);
Ext.data.JsonP.callback3([{"title":"ssdfds"}]);


here is a PHP-Script if you want to test it:


<?php

$user = array(array('name' => 'dfgfd'));
echo $_GET['callback'] . '(' . json_encode($user) . ');';

?>

<?php

$post = array(array('title' => 'sdffdfds'), array('title' => 'dfgdfgdg'));
echo $_GET['callback'] . '(' . json_encode($post) . ');';

?>

lenoxDoe
16 Apr 2013, 8:39 AM
Hi,

here is an example on JSFiddle for you: http://jsfiddle.net/lenoxDoe/n6Xbw/2/