PDA

View Full Version : Getting store values from another function



redjetfire
9 Oct 2012, 10:54 PM
loadStore: function()
{
var store = this.getProfilesStore();
store.load();
store.on('load', function(store, records)
{
var data = records[0].data;
});

viewProfile: function()
{
this.loadStore();
}


How can i get var data values in viewProfile function ?

vietits
9 Oct 2012, 11:09 PM
You could return the 'data' variable so that it would be given to the 'viewProfile' function when 'loadStore' is called. Here you go,



loadStore: function()
{
var data;
var store = this.getProfilesStore();
store.load();
store.on('load', function(store, records)
{
data = records[0].data;
});
return data;
}


viewProfile: function()
{
var data =this.loadStore();
}


Regards,
Premkumar. A
In viewProfile, the returned data from calling this.loadStore() will be null because store loading is asynchronous. data in loadStore() will contains value only when store finishes its loading data from server.

vietits
9 Oct 2012, 11:21 PM
@a.premkumar,

No. It's still the same.

@redjetfire,
Maybe this is what you want:


loadStore: function(callback){
var store = this.getProfilesStore();
store.on('load', callback);
store.load();
// store.on('load', function(store, records) {
// var data = records[0].data;
// });
}


viewProfile: function(){
this.loadStore(function(store, records){
var data = records[0].data;
// do something on complete loading data
});
}

a.premkumar
9 Oct 2012, 11:21 PM
In viewProfile, the returned data from calling this.loadStore() will be null because store loading is asynchronous. data in loadStore() will contains value only when store finishes its loading data from server.

Then how could we do it? By adding delay time? or we have to define a callback?



store.load({
scope: this,
callback: function(records, operation, success) {
data = records[0].data;
}
});

a.premkumar
9 Oct 2012, 11:24 PM
Thankyou veitits

redjetfire
9 Oct 2012, 11:38 PM
@a.premkumar,

No. It's still the same.

@redjetfire,
Maybe this is what you want:


loadStore: function(callback){
var store = this.getProfilesStore();
store.on('load', callback);
store.load();
// store.on('load', function(store, records) {
// var data = records[0].data;
// });
}


viewProfile: function(){
this.loadStore(function(store, records){
var data = records[0].data;
// do something on complete loading data
});
}


Yes its work
The problem is when I called loadStore from second function ( viewSettings) its call loadStore and viewProfile and begin sibling on viewProfile instead viewSettings with Error

vietits
9 Oct 2012, 11:46 PM
If so, you should modify loadStore() as below:


loadStore: function(callback){
var store = this.getProfilesStore();
// store.on('load', callback);
store.load({
callback: callback
});
// store.on('load', function(store, records) {
// var data = records[0].data;
// });
}

redjetfire
10 Oct 2012, 12:39 AM
If so, you should modify loadStore() as below:


loadStore: function(callback){
var store = this.getProfilesStore();
// store.on('load', callback);
store.load({
callback: callback
});
// store.on('load', function(store, records) {
// var data = records[0].data;
// });
}


It doesnt work (

vietits
10 Oct 2012, 1:36 AM
It work, however you have to change the argument list of callback function.

1. Solution 1:


loadStore: function(callback){
var store = this.getProfilesStore();
store.load({
callback: callback
});
}


viewProfile: function(){
// this.loadStore(function(store, records){
this.loadStore(function(records){
var data = records[0].data;
});
}


2. Solution 2


loadStore: function(callback){
var store = this.getProfilesStore();
store.on('load', callback, this, {single: true});
store.load();
}


viewProfile: function(){
this.loadStore(function(store, records){
var data = records[0].data;
});
}

redjetfire
10 Oct 2012, 1:56 AM
Thanks All