Trying to pass values to local storage using Sench

2019-08-10 08:25发布

问题:

Can anyone tell me why this bit of code doesn't create an entry into localstorage?

    myLocalStore.load();

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

    var entry1 = { id: cardId, dateCreated: now, title: 'The Title', narrative: 'the Text' };
    var newRecord = new PinYin.model.Default(entry1);
    myLocalStore.add(newRecord);

    myLocalStore.sync();

    console.log(entry1);

The console outputs

Object{id: "136575772251069", dateCreated: Fri Apr 12 2013 17:08:42 GMT+0800 (Taipei Standard Time), title: "The Title", narrative: "the Text"}

which is the correct format so im not sure what I'm doing wrong...

if I replace myLocalStore.add(entry1); with myLocalStore.add('value');

then i'll get the following inputted into the local storage

{"id":"ext-record-1","dateCreated":null,"title":null,"narrative":null}

回答1:

I faced the same situation and found the solution like that,

First of all, if you set "idProperty" field of model, remove it.

After that, use the following convention.

myOfflineStore.add([{ listItemId: record.data.listItemId, 
                      listItemTitle: record.data.listItemTitle, 
                      listItemImageCode: record.data.listItemImageCode,     
                      listItemNewPostCount: record.data.listItemNewPostCount }]);

Using [, ] is the key point.

EDIT: If you want to use idProperty, then you need to set the newly added record as "phantom" with setting phantom = true



回答2:

Try like this...

      var airline_name = res[i].AirLineName; 
localStorage.setItem("AirlineCode3two",record.get('AirlineCode32'));

        var stored_locally2 = {'ID2': i,'AirLineName2': res[i].AirLineName,};

             //Adding our array to LocalStore(localStorage)
                         var localstore2 = Ext.getStore('TwowayStorage');

                         var twowayc1=localstore2.getCount();
                         localstore2.add(stored_locally2);
                         localstore2.sync();
                         localstore2.load();