PDA

View Full Version : Sencha Touch 2.0.1.1 Local Store not storing values



JesusJoseph
23 Nov 2012, 3:40 PM
Hi All,
I am trying to store in the local storage and its not getting stored:(.
I need to store two values as state id and license type as 1 record and next time I will update it. So the store will always has 1 record like below.



{
'stateId': 'NJ',
'licenseType': 'BDL'
}


I tried for the last 2 days and with different option but its not storing.
Can someone please tell me what can be the problem in my code.



Note: I am trying to test is using Chrome browser.

Store Code

Ext.define("Sencha.store.TestParams", { extend: "Ext.data.Store",
requires:
[
"Ext.data.proxy.LocalStorage"
],
config: {
model: "Sencha.model.TestParam",
autoSync: true,
proxy: {
type: 'localstorage',
id: 'ezdt-test-params'
}
}
});

Model Code


Ext.define("Sencha.model.TestParam",
{
extend: "Ext.data.Model",
config:
{
idProperty: 'id',
fields:
[
{ name: 'id', type: 'int' },
{ name: 'stateId', type: 'string' },
{ name: 'licenseType', type: 'string' }
]
}
});




Actual Code to save


var modTestParamInitialValues = Ext.create("Sencha.model.TestParam", {
id: 'ezdt', //Always Same as there will be only one record.
stateId: strSelectedState,
licenseType: strSelectedLicenseType
});
storTestParams.add(modTestParamInitialValues);
storTestParams.sync();


Code I am using to retrieve the store value.
Always its printing as Not Set


var storTestParams = Ext.getStore("TestParams");
console.log("storTestParams#####-->" + storTestParams);
//storTestParams.load();
if(null != storTestParams)
{
var intTestParamCount = storTestParams.getCount();
if(intTestParamCount == 0)
{
console.log("Not Set");
}
else
{
console.log("Set Correctly");
}
}
else
{
console.log("Set Correctly");
}


I hope I give all the details. If you need any more details please let me know.

Thanks for your help.
Joseph

wuytsr
26 Nov 2012, 3:31 AM
In your model, the ID field is set as an integer.

When you create a model, you set 'ezdt' as the ID. But 'ezdt' is not an int, but a String. Maybe that's the problem.

Try this in your model:


fields: [
{ name: 'id', type: 'auto' },
{ name: 'stateId', type: 'string' },
{ name: 'licenseType', type: 'string' } ]

Do you use the developper tool in chrome? (Ctrl-Shift-I)
If not, you really should! Because in the "Resources" tab, you'll find all the Local Storage objects that are currently stored, and many other really helpfull features!

This is the way I store an object in my LocalStorage:



console.log("Try to store something");




userstore = Ext.getStore('LocalStorageUserStore');


var model = new App.model.LocalStorageUser({
id: '1',
user: "username",
pwd: "password"
});
var records = userstore.add(model);
records[0].save();

userstore.sync();


var user = userstore.getAt(0).data.user;
console.log(user);
var pwd = userstore.getAt(0).data.pwd;
console.log(pwd);

I'm not saying this is the way you have to store something in your localstorage, but this code just works fine for me. :)

JesusJoseph
28 Nov 2012, 7:54 AM
I use the same code and still I am getting the below error for the line records[0].save();:((
Can you please tell me what proxy you use in the model.

This is the code that I use to save the file.



var now = new Date();
var noteId = (now.getTime()).toString() + (this.getRandomInt(0, 100)).toString();


var newNote = Ext.create("NotesApp.model.Note", {
id: noteId,
dateCreated: now,
title: "Joseph",
narrative: "Joseph Desc"
});
var records = notesStore.add(newNote);
records[0].save();
notesStore.sync();





Uncaught Error: [ERROR][NotesApp.controller.Notes#onSaveNoteCommand] You are trying to save a model instance that doesn't have a Proxy specified

wuytsr
28 Nov 2012, 8:03 AM
This is the code:

Ext.define(
'App.model.LocalStorageUser', {

extend: 'Ext.data.Model',

alias: 'widget.LocalStorageUser',

config: {

idProperty: 'id',

fields: [

{ name: "id", type: "auto" },

{ name: "user", type: "string" },

{name: "pwd", type:"string"}

],

proxy: {

type: 'localstorage',

id: 'users'

}

}

});