Unable to use setTitle method when using a string concatenated with datastore data
I have the following code attached as a handler to a button:
myapp.stores.vehicleInfo.load();
myapp.toolbars.vehicleInfoToolbar.setTitle('Image 1 of ');
Which within my defined namespace loads the store and then sets the title of a toolbar - this works.
This code doesn't work:
myapp.stores.vehicleInfo.load();
myapp.toolbars.vehicleInfoToolbar.setTitle('Image 1 of' + myapp.stores.vehicleInfo.data.items[0].data.imageTotal);
giv开发者_如何学编程ing the error: 'cannot read property of "data" of undefined'
But when I click the button a second time, it sets the title of the toolbar as expected e.g. Image 1 of 10.
What is going on here? How do I solve this? I tried using the settimeout method in case the setTitle method was being called too early before the store was finished loading. However this failed to solve the problem.
Thanks.
the store load() method is asynchronous and it usualy takes a while to load the store so ... you should set the title on the callback function when you are certain you have the data
myapp.stores.vehicleInfo.load({
callback:function(r, options, success) {
myapp.toolbars.vehicleInfoToolbar.setTitle('Image 1 of' + myapp.stores.vehicleInfo.data.items[0].data.imageTotal);
}
});
The store is not loaded yet when the setTitle
is called. You should put call to setTitle
into load
's callback:
myapp.stores.vehicleInfo.load({callback : function(records){
myapp.toolbars.vehicleInfoToolbar.setTitle('Image 1 of' + records[0].data.imageTotal);
}});
精彩评论