Model defines your business entity and proxy allows you to create CRUD (create, read, update, delete) functions for the models. Model is assigned to store. Here in this blog I am going to explain how to use models and proxy with store.
First define a proxy.
Ext.data.ProxyMgr.registerType(“myProxy”,
Ext.extend(Ext.data.Proxy, {
create: function (operation, callback, scope) {
//Here goes your create logic
},
read: function (operation, callback, scope) {
//Here goes your read logic
},
update: function (operation, callback, scope) {
//Here goes your update logic
},
destroy: function (operation, callback, scope) {
//Here goes your delete logic
}
})
);
This will create a new proxy with a name my proxy. Now create a model and assign proxy to it.
Ext.regModel(‘myModel’, {
fields: [
{name : ‘field1’ , type : ‘int’},
{name : ‘field2’ , type : ‘datetime’},
{name : ‘field3’ , type : ‘string’} ],
proxy: {
type: “myProxy”
}
});
But how do you read the value of a record or field? Every example in the whole damn world just says load().
store load method will call read method of proxy. If you just want to read record in store. you can get record using getById() function if you set id property in model one you have record you can read field value using record.get('fieldname')
Rather than using proxy you can also store the data in a local database. it becomes easy to just query rather than update using proxies
It depends on scenario. Sometimes we can have local database like sqllite but in case of APIs we have to depend on proxy to get and send data.
Why myStore.sync(); on the add method and not the others. What exactly does this function do?
Sync function will add record in localStorage.