insert in sencha touch data store

雨燕双飞 提交于 2019-12-11 08:04:34

问题


Could someone please explain how the insert works to add a record in a datastore with tha fields: "title", "info" and "price"? because i tried some things and none of them work. and the sencha website doesnt make it very clear.


回答1:


Adding a new item to an existing Store isn't that hard actually.

First of you will need to configure your model and store. In your question you name the fields 'title, 'info' and 'price'.

Model:

Ext.regModel('myModel', { 
    fields: [
        {name: 'id', type: 'int' },
        {name: 'title', type: 'string' },
        {name: 'info', type: 'string' },
        {name: 'price', type: 'int' }           
    ]
});

Next you configure the store that will hold the data, based on the above model. I think that, in your case, it should be a model without any data preloaded via, for example, JSON?

So lets make a localstorage (empty store). The Store consists of the model (myModel), you give it a storeID (so that you can later on reference the store by this ID). The proxy is localstorage and the unique ID of the Store will be the ID field of the Model.

Store:

    var myStore = new Ext.data.Store({ 

        model: "myModel",
        storeId: "myStoreID",
        proxy: {
            type: "localstorage",
            id: "id"            
        }
    });

Now, suppose you have some kind of Form (in which the user can add input a title, info and price, and you want to add these items to the existing store on submittal.

Within the handler of the submittal button you now have to 'call' the store, and perform the add function on it. Within this add function you will have to define the params (the model params) and the data to insert.

Below I have used a mixture of fixed data and a variable to insert.

myStoreID.add({ title: "Mijn Titel", info: "Informatie, price: prijsvar });

The store will now be filled will now be filled with an extra data-record which you can use. Lets say for example that the store is attached to a dataview, then you can perform:

dataView.update();

The above isn't a full tutorial, but I think this will help you along?




回答2:


Just an update of the YDL answer. As per the dataView should be related to the updated store, the last sentence dataView.update() should not be needed, due to the automatic update of the views related to a store when it change.

new Ext.DataView({
    store: MyStore,
    itemSelector: 'div.thumb',
    tpl: thumbTpl
});

later, if I do the following, the new item should be displayed in views (List, DataView, etc.) that have MyStore as store.

MyStore.add(newItem);

HTH. Milton Rodríguez.




回答3:


If you are trying to pass in an object that was returned from a getValue() on your form, make sure that you run a

myStore.sync(); 

after you have called the add() method, or you wont see it in your browsers local store.




回答4:


It is Very easy try these

// first get those values and store in locally

     var A_select1=Ext.getCmp('select1').getValue();  // get value

      localStorage.setItem("Adult1_select1",A_select1); // set localStore


      var AdultSalutation={

                     'Adult1_select1':A_select1,    // assign the value                                                   

            };                                       

    var AdultSalutationstore = Ext.getStore('Adult_AdultSalutationstore'); // get store

        AdultSalutationstore.add(AdultSalutation);  // add object   

        AdultSalutationstore.sync();    // sync 

        AdultSalutationstore.load();    // load                                                 


来源:https://stackoverflow.com/questions/6068585/insert-in-sencha-touch-data-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!