ExtJs Ext.data.Model 学习笔记
Using a Proxy
Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], proxy: { type: 'rest', url : '/users' } });
当在Model中定义了一个Proxy以后就可以使用 save,update,load,destroy这4个方法 进行增删改查操作
var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed@sencha.com'}); user.save(); //POST /users
//get a reference to the User model class var User = Ext.ModelManager.getModel('User'); //Uses the configured RestProxy to make a GET request to /users/123 User.load(123, { success: function(user) { console.log(user.getId()); //logs 123 } });
//the user Model we loaded in the last snippet: user.set('name', 'Edward Spencer'); //tells the Proxy to save the Model. In this case it will perform a PUT request to /users/123 as this Model already has an id user.save({ success: function() { console.log('The User was updated'); } }); //tells the Proxy to destroy the Model. Performs a DELETE request to /users/123 user.destroy({ success: function() { console.log('The User was destroyed!'); } });
commit( [silent], [modifiedFieldNames] ) 该方法用于提交Model的修改.会通知Store,在gridPanel中 model修改以后会有红色标记,该方法会提交修改.
phantom: Boolean 该属性标记该 Model实体是否是 新的 如果没有ID 这为true, 否则为false. 如果在前台gridPanel中手动加入Model 那么有又ID的话 可以手动设置为false
Ext.define('MyApp.data.MyModel', { extend: 'Ext.data.Model', requires: ['Ext.data.UuidGenerator'], idgen: 'uuid', ... });
可以给Model设置ID生成策略 这样就可以New出来的Model在通过Store.sync();就可以调用Insert方法了。
不然有ID的Model Store会认为不是新数据 那么通过Store.getModifiedRecords() 拿不到。用过上述的ID自动成功策略可以解决此问题