PDA

View Full Version : SOLVED: Load store from a variable.



gergar28
19 Apr 2012, 11:18 AM
SOLVED:
Hi again.
Im tryin to load a storage from a variable. in this moment i'm work in the same way.

in app.js (i have the default MVC architecture) i define "respJson":


var me = this;
var respJson = null;

Later, in the "launch: function()" block i make a xml ajax request, and this response i convert to json, and store in respJon like that:



launch: function() { // Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();

Ext.Ajax.request({
url: 'http://localhost:8081/someApp/SomeWebServiceResponseXml',
params: {
component: 'SomeParam',
userid: 'id',
password: 'pass'
},
method: 'GET',
success: function(response, opts) {

me.respJson = Ext.JSON.encode(me.ConvertXmlToJson(response.responseXML);
console.log(me.respJson);

}
});


// Initialize the main view
Ext.Viewport.add(Ext.create('App.view.Main'));
}

Now in my store (i'm use a proxy memory) have the following code:
(MyApp/app/store/myStore.js)


Ext.define('App.store.myStore', {
extend: 'Ext.data.TreeStore',

requires: [
'App.model.myModel'
],


config: {
autoLoad: true,
model: 'App.model.myModel',
data: me.respJson, //For some reason my var "respJson" is null (i do a alert(me.respJson); )
proxy: {
type: 'memory',
reader: {
type: 'json',
rootProperty: 'file'
}
}

}
});



I try to call respJson i this way too: data: 'MyApp.app.respJson', and i get the same result (null)

Note: if i take the content of respJson, copy and paste in some file "data.json" and call it in this way:

proxy: { type: 'ajax',
url: 'resources/data/data.json',
reader: {
type: 'json',
rootProperty: 'file'
}
}

My app work awsome, but is not the solution that i want :(.

Why my respJson variable are initialized?
Some help to use global variables?, im search for "global variables" in the forums and i only find what i call "global constant".

Thanks in advance!
Gerardo garrido.

gergar28
19 Apr 2012, 12:41 PM
I resolved this problem in this way.
In the load: function() block y add the followings lines:



if(Ext.getStore('turboStore') != null){
console.log("se lee el store");
var store = Ext.getStore('turboStore');
store.setData(me.respJson);
Ext.getStore('turboStore').sync();
}

Now my launch: function is like that:



launch: function() { // Destroy the #appLoadingIndicator element
Ext.fly('appLoadingIndicator').destroy();

Ext.Ajax.request({
url: 'http://localhost:8081/someApp/SomeWebServiceResponseXml',
params: {
component: 'SomeParam',
userid: 'id',
password: 'pass'
},
method: 'GET',
success: function(response, opts) {

me.respJson = Ext.JSON.encode(me.ConvertXmlToJson(response.responseXML);
console.log(me.respJson);

if(Ext.getStore('turboStore') != null){ //The magic! console.log("se lee el store");
var store = Ext.getStore('turboStore');
store.setData(me.respJson);
Ext.getStore('turboStore').sync();
}

}
});




// Initialize the main view
Ext.Viewport.add(Ext.create('App.view.Main'));
}


Finally i deleted the "data" parameter in myStore and add an storeId, like that:



Ext.define('App.store.myStore', {
extend: 'Ext.data.TreeStore',

requires: [
'App.model.myModel'
],


config: {
autoLoad: true,
model: 'App.model.myModel',
storeId: 'turboStore',
proxy: {
type: 'memory',
reader: {
type: 'json',
rootProperty: 'file'
}
}

}
});


Thx for read :)