PDA

View Full Version : How to set a model proxy and store in runtime.



ericchaves
7 Apr 2012, 6:46 AM
Hi Folks,

I'm using Jasmine (actually Jasmine-Species) to learn Sencha Touch 2 through test code snippets. I wrote a model with some validations and some associations and so far code is running ok.

I don't want to define the proxy in the model itself, since I believe that the decision of which kind of store/proxy to use should be handled by the controller.

Now I'd like to save some model instances in session storage so I can inspect it but somehow I'm failing to do it. I have tried to it in several different ways all ending in error messages. I tried to create the store before creating an instance of the model like this:


var user, id, errors;
var store = Ext.create('Ext.data.Store', {
model: 'User'
,proxy: {
type: 'sessionstorage'
,id : 'testProxy'
,autoload: true
,reader: {
type: 'json'
}
}
});

beforeEach(function() {
user = Ext.create('QPay.model.User', {username: 'ericchaves', email: 'eric+teste@craftti.com.br', password: '123456'});
});


This end-up with a error message saying that the my model does not exists but if I try to create a model instance it is created without problem.

I also tried to create the store after creating an model instance:


user = Ext.create('QPay.model.User', {username: 'ericchaves', email: 'eric+teste@craftti.com.br', password: '123456'});

var store = Ext.create('Ext.data.Store', {
model: 'User'
,proxy: {
type: 'sessionstorage'
,id : 'testProxy'
,autoload: true
,reader: {
type: 'json'
}
}
});

var idStore = user.identities();
id = idStore.add({CPF: '12345678901', fullName: 'Eric Chaves'});
user.save();




In this case when I call user.save() I got two error messages: the first saying that the Model user does not exists and second saying that I'm trying to save I model without a proxy in it.

What am I doing wrong here? How should I do to crate a store during runtime and use it with my models?

Thanks in advance,

Eric

ericchaves
7 Apr 2012, 8:59 AM
Ok. Kinda figure it out (I guess). I'm creating the proxy, creating the model instance and then setting the instance's proxy. This way when I call instance.save() it works. Sample code below:


var proxy = Ext.create('Ext.data.proxy.SessionStorage', {id: 'proxyTest'});
var user = Ext.create('QPay.model.User', {username: 'ericchaves', email: 'eric+teste@craftti.com.br', password: '123456'});
user.setProxy(proxy);
var idStore = user.identities();
id = idStore.add({CPF: '12345678901', fullName: 'Eric Chaves'});
idStore.sync();
user.save({
success: function(obj){console.log('obj saved: %o', obj)}
});
console.log('user: %o id: %o store: %o', user, id, idStore);



My problem now is that this is not saving the instance associations, only the root object. In the example above the user object is saved by without any identity.

Do I also need to save the association's instance? Should not it be saved automatically since it belongs to the user?

Best regards,

Eric

ericchaves
7 Apr 2012, 11:31 AM
Ok, just now I realized that saving associations is not yet supported. more info at http://www.sencha.com/forum/showthread.php?173327.

cheers all.