lundi 9 juin 2014

Global store session

I have problems understanding how Ext.data.Session should be used. I've been reading forums, but could not find anything useful. Is there a way to define a global session? Can/should we load ALL the data in a global session?

In my case, I have a global Location store defined on the application, which I autoLoad from the server, together with a bunch of other global entities. When all the data is ready, I loop through all locations and manually display markers on the map. When a user clicks on a marker, I want to link the record to some viewModel, but every time I do that, a new instance is requested from the server via the direct proxy. It works OK, but I want it to load the record from the global Location store, which already holds that instance.


After some debugging I see that viewModel.linkTo() uses session to see if the instance is already present, otherwise loads it from the server. I did manage to add all the records to the session by doing session.adopt(location), but this feels wrong.



Code:



locationStore.each(function(location){
session.adopt(location);
}, this);

Here's some code, I hope someone gets it

Model



Code:



Ext.define('APP.model.Location', {
extend: 'APP.model.Base',

fields: [
{name: 'name', type: 'string'},
{name: 'description', type: 'string'},
{name: 'lat', type: 'float'},
{name: 'lng', type: 'float'},
{name: 'created', type: 'date'}
],

proxy: {
type: 'direct',
api: {
create : 'APP.api.Location.save',
read : 'APP.api.Location.read',
update : 'APP.api.Location.update',
destroy : 'APP.api.Location.destroy'
},
reader: {
type: 'json'
}
}
});

Store


Code:



Ext.define('APP.store.Locations', {
extend: 'Ext.data.Store',

storeId: 'Locations',

model: 'APP.model.Location',

autoLoad: true
});

View


Code:



Ext.define('APP.view.main.Main', {
extend: 'Ext.tab.Panel',

xtype: 'app-main',

controller: 'app-main',

viewModel: {
type: 'app-main'
},

session: {},

reference: 'app-main'
});

ViewModel


Code:



Ext.define('APP.view.main.MainModel', {
extend: 'Ext.app.ViewModel',

requires: [
'APP.model.Location'
],

alias: 'viewmodel.app-main',

stores: {
locations: {
model: 'Location',
autoLoad: true
}
}

// also tried this
/*
stores: {
locations: {
source: 'Locations',
autoLoad: true
}
}
*/
});

ViewController


Code:



Ext.define('APP.view.main.MainController', {
extend: 'Ext.app.ViewController',

alias: 'controller.app-main',

init: function() {
var view = this.getView(),
viewModel = view.getViewModel(),
session = this.getSession(),
store = this.getStore('locations');

console.log('view', view);
console.log('viewModel', viewModel);
console.log('session', session);
console.log('store', store);

Ext.defer(function(){
/*
// add records to session manually
// this actually prevents viewModel.linkTo() to fetch the instance from server, which is what I want
store.each(function(location){
session.adopt(location);
}, this);
*/

var location = store.getById(482);

viewModel.linkTo('location', location); // fetches location from server

// tried this, also fetches record from server
/*
viewModel.linkTo('location', {
reference: 'Location',
id: 482
});
*/
}, 1000, this);
}
});

So what is the proper way to use session? How do we read records into session? The docs @http://ift.tt/1mzpOuZ are not helpful ...

Also a side question ... if I define location store on main view model, I can access it on main view controller via this.getStore('location'). I know I can bind to location store in child views, but how can I access location store on child view controller?





Aucun commentaire:

Enregistrer un commentaire