insert in sencha touch data store
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?
Marijn van de Sande
Updated on November 20, 2022Comments
-
Marijn van de Sande over 1 year
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.
-
Marijn van de Sande almost 13 yearsthank you so much and can i also show the data in a list?? and i want to use it when click on the onItemDisclosure but it doesnt seem to work i typed this: onItemDisclosure: function () { WinkelwagenStore.add({title: "Mijn Titel", info: "Informatie", prijs: "25"}) }
-
YDL almost 13 yearsWhat doesn't work exactly? The renewal of the data in the list? Or the adding to the store? Put a "console.log(WinkelwagenStore) at the end of your function to check whether the store is updated.