-
7 Nov 2012 5:31 AM #1
Answered: Localstorage access first()
Answered: Localstorage access first()
Hi there,
I want to access the first item in a localstorage.
Here is my model:
Saving a record:Code:Ext.define('AppData', { extend: 'Ext.data.Model', config: { identifier: 'uuid', proxy: { type: 'localstorage', id: 'appData' }, fields: [{ 'id', 'name' }] } });
The id is something like ext-record-1417Code:var myApp =Ext.create('AppData',{ name:'Ed Spencer' }); myApp.save({ success:function(ed){ console.log("Saved Ed! His ID is "+ ed.getId()); } });
Here is how I want to load the first record:
This always throws the failure.Code:var app = Ext.ModelMgr.getModel('AppData'); app.load(1, { success: function(app) { console.log("Loaded app 1: " + app.get('name')); }, failure: function() { console.log("NOT loaded app 1"); } });
I tried to use the example from the official example http://docs.sencha.com/touch/2-0/#!/guide/models.
But with the localstorage this does not work either.
Any idea?
Best
-
Best Answer Posted by haduki
var id=localStorage.getItem('personalData').split(',')[0];//<-pass the localstorage proxy id config.
-
7 Nov 2012 6:24 AM #2
You could try something like this..
myStore.getData().items[0]
-
8 Nov 2012 12:40 AM #3
without store
without store
Hi ,
I do not have a store for the data.
-
8 Nov 2012 2:14 AM #4
I use this in my app with a Store too:
Store:
and then when I need the modelCode:.. config: { storeId: 'credentialsStore', ..
Code:var credentials = Ext.getStore('credentialsStore').first(); credentials.get('name');
-
8 Nov 2012 3:32 AM #5
Thanks but again: I do not want to use a store. This is a simple update function. The old version had a store, but only used a single record. Now I want to import the data from that first record.
I could create a store and destroy it right after the import, but I was wondering if there is a way without using a store.
-
8 Nov 2012 3:49 AM #6
what does first mean? you order by what?
id != indexCode:model.load(id,config)
as you said
so why you pass a count or index number?The id is something like ext-record-1417I write English by translator.
-
8 Nov 2012 7:12 AM #7
-
8 Nov 2012 7:19 AM #8
pass the id
I write English by translator.
-
8 Nov 2012 7:29 AM #9
I'm guessing it's because that id changes every time he uses the app as it is one automatically generated by Sencha. Can you not add your own id to it?
-
8 Nov 2012 7:43 AM #10
@haduki
how do i find out the id?
This is an update of the app. So the old app wrote the localstorage and now I want to load the localstorage in the model. If I write the id I would write a new record, which I could load afterwards.
But for now I do not know the id (auto-generated by Sencha).
so what I am doing for now is:
This will do the trick, but I was hoping to go without the store.Code:Ext.define('oldData', { extend: 'Ext.data.Model', config: { identifier: 'uuid', fields: [ {name: 'id', type: 'int'}, {name: 'oneOfManyValues'} ], proxy: { type: 'localstorage', id: 'personalData' } } }); var myStore = Ext.create('Ext.data.Store', { model: 'oldData', autoLoad: true }); var oldData = myStore.first() ? myStore.first().getData() : null; Ext.StoreManager.remove(myStore);


Reply With Quote