1.定义Model
Ext.define('ExtJs5Example.model.Student', {
extend: 'Ext.data.Model',
alias: 'widget.Student',
fields: [
{ name: 'StudentID', type: 'string' },
{ name: 'Name', type: 'string' },
{ name: 'Age', type: 'string' }
],
proxy: {
type: 'ajax',
api: {
create: '/Home/SaveSudentInfo',//当调用Model.save()方法时会调用这个方法
read: '/Home/GetStudentInfo'//<span><span class="comment">//在调用Model的静态方法load时,会默认去这里查数据</span><span> </span></span>
},
reader: {
type: 'json',
successProperty: 'success',
rootProperty: 'data',
totalProperty: 'total',
messageProperty: 'message'
},
listeners: {
exception: function (proxy, response, operation) {
Ext.MessageBox.show({
title: 'REMOTE EXCEPTION',
msg: operation.getError(),
icon: Ext.MessageBox.ERROR,
buttons: Ext.Msg.OK
});
}
}
}
});
2.获取form的数据,给Model赋值,调用model的save 方法
//获取itemId为StudentInfo的form中所有的数据
var values = Ext.ComponentQuery.query("StudentInfo")[0].getValues();
//定义model对象
var record = Ext.create('ExtJs5Example.model.Student', {});
//将从form中获取的值赋到model对象中
record.set(values);
//调用model对象的save方法
record.save({
success: function (form, operation) {
Ext.MessageBox.show({
title: '提示',
msg: operation._resultSet.message,
icon: Ext.MessageBox.OK,
buttons: Ext.Msg.OK
});
},
failure: function (form, operation) {
Ext.Msg.alert('提示', operation._error);
}
});