samedi 7 février 2015

Cannot save model through store

I'm sure this is pretty basic stuff but I'm new to Sencha Touch and could use some help.

I'm using Sencha Architect and have created a "Users" store and a "User" model:



Code:



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


requires: [
'APP.model.User',
'Ext.data.proxy.LocalStorage'
],


config: {
autoLoad: true,
autoSync: true,
model: 'APP.model.User',
storeId: 'Users',
proxy: {
type: 'localstorage',
id: 'user_login'
}
}
});


Code:



Ext.define('APP.model.User', { extend: 'Ext.data.Model',


requires: [
'Ext.data.Field',
'Ext.data.proxy.LocalStorage'
],


config: {
idProperty: 'user_login',
fields: [
{
name: 'user_login'
},
{
name: 'user_data'
}
],
validations: [
{
type: 'email',
field: 'user_login'
},
{
type: 'presence',
field: 'user_login'
},
{
type: 'presence',
field: 'user_data'
}
],
proxy: {
type: 'localstorage',
id: 'User'
}
}
});

The following works to save data and to be able to look it back up again:


Code:



var user = Ext.create('APP.model.User', {
user_login: 'user@address.com',
user_data: user_data
});
user.save();

// some time later
var UserStore = Ext.getStore('Users');
var value = UserStore.find('user_login','user@address.com');
console.log(value); //returns 1 (index of found entry)

I assume that I should be saving this to the store as follows instead of directly working with a model:


Code:



var UserStore = Ext.getStore('Users');
var UserToAdd = { user_name: 'user@address.com', user_data: user_data };
UserStore.add(UserToAdd);
UserStore.sync();

But when I do it that way it doesn't save to the localstorage and cannot be pulled up again.

What am I doing wrong?

Should I continue doing it the way that works but looks to be the "wrong" way according to docs? or should I do it differently?

Am I completely misunderstanding how stores and models work?


Thank you.






Cannot save model through store

Aucun commentaire:

Enregistrer un commentaire