PDA

View Full Version : Can't save into store



bagusflyer
3 Jan 2012, 5:16 AM
After solving a few problems, at last I can see my view but I came across another problem: I can't save my data (model) into a store. Here is my code:




var currentUser= this.profileView.getRecord();
this.profileView.updateRecord(currentUser);


var errors = currentUser.validate();
if (!errors.isValid()) {
currentUser.reject();
Ext.Msg.alert('Wait!', errors.getByField('name')[0].message, Ext.emptyFn);
return;
}


// save user profile to data store
var store = this.getStore('App.store.MyProfile');
if (null == store.findRecord('id', currentUser.data.id)) {
store.add(currentUser);
} else {
currentUser.setDirty();
}




Almost the same code running in Sencha Touch 1.1 has no problem. But failed at Sencha Touch 2. The error message is:

Uncaught TypeError: Cannot call method 'create' of undefined

It's either in this.profileView.updateRecord(currentUser); or store.add(currentUser);. I checked the store and currentUser, they seems to be correct. What could be wrong? (My store's autoSync is set to true, so I think I don't need to call store.sync() after the store.add())

Thanks

mitchellsimoens
3 Jan 2012, 5:29 AM
If the store.add is failing then autoSync wouldn't work so there would be no request. It's kind of hard to debug something for someone when they don't step through to figure out what line is failing.

bagusflyer
3 Jan 2012, 6:05 AM
Here is the call stacks:

30428

Does it help? I totally got no clue.

Thanks

mitchellsimoens
3 Jan 2012, 6:09 AM
store.add looks like it's ok, it's the syncing that isn't working.

bagusflyer
3 Jan 2012, 7:05 AM
Yes. I believe so. When I turn off autoSync, the code will not fail until I add store.sync at the end. So the problem must be related to sync. I'll debug more. Thanks

bagusflyer
4 Jan 2012, 7:19 AM
I change the autoSync to false and manually run store.sync(). Yes, it failed in store.sync().

Here is the code inside sync() function in sencha-touch-all.js:



sync: function() {
if (typeof this.proxy.sync != 'function') {
this.callParent(arguments);
}else{
this.proxy.sync(this);
}
},



if (typeof this.proxy.sync != 'function') return true, so this.proxy.sync(this); is never been called. I don't know whether that's the problem.

Here is my source code for the store:



Ext.define('App.store.MyProfile', {
extend : 'Ext.data.Store',


model : 'App.model.User',
autoLoad : true,
//autoSync : true,

proxy: {
type: 'localstorage',
id: 'my-profile-store'
}
});


Somebody mentioned the localstorage in Sencha Touch 2.0 is broken. Is this true? If this is the cause, can I use JSON and save my data back to JSON file in server?

Thanks

bagusflyer
4 Jan 2012, 4:01 PM
Found a workaround:http://www.sencha.com/forum/showthread.php?156384-localstorage-Uncaught-TypeError-Cannot-call-method-create-of-undefined&highlight=Localstorage

Thanks , I'll try that.