译自:http://docs.sencha.com/extjs/4.2.3/#!/api/Ext.app.Controller
A template method that is called when your application boots. It is called before the Application's launch function is executed so gives a hook point to run any code before your Viewport is created.
This is a template method. a hook into the functionality of this class. Feel free to override it in child classes.
这是一个模板方法(模板方法会在应用的特定阶段自动被调用),这个方法会在应用启动时被调用,而且会在应用的launch方法执行前被调用,是在你的Viewport创建前执行一些初始化代码的入口方法,
通常会在子类中覆盖这个初始化方法。
例子:
1、index.js:
Ext.application({
name : 'cdkj',
appFolder : base + '/static/js/com',
paths : base + '/static/js/com',// 设置路径信息
// controllers : [ 'index.MainController' ],
controllers : [ 'login.UserLogin' ],
// controllers : [ 'UserLogin', 'MainController', 'DeptListController' ],
launch : function() {
Ext.tip.QuickTipManager.init();
// Ext.create('Ext.container.Viewport', {// 当前页面加载完成做的事情
// layout : 'auto',// 自动布局
// html : '欢迎登录XXX系统',
// });
}
});
2、ArchiveListController.js:
//矫正档案列表
Ext.define('cdkj.controller.message.ArchiveListController', {
extend : 'Ext.app.Controller',
// views : [ 'message.ArchiveListView', 'message.ArchiveButton',
views : [ 'message.ArchiveListView', 'sys.DeptTreeView',
'message.ArchiveButton', 'message.ArchiveContainer',
'message.ArchiveAddMain', 'message.ArchiveRelationMain',
'message.ArchiveBaseAll', 'message.ArchiveBaseInfo',
'message.BaseSearchWithDpetView',
'sys.ComboHaveNoneView',
'sys.ComboDictionaryView'],
stores : [ 'message.ArchiveStore', 'sys.DeptTreeStore'
,'sys.ComboHaveNoneStore'
,'sys.ComboDictionaryStore'
],
models : [ 'message.ArchiveModel', 'sys.DeptModel'
,'sys.ComboxModel'
],
//便于获取组件,名字为与别名相同,下面可以直接用getXXX()方法,类似Ext.ComponentQuery.query()功能。
refs :[{ref:'messageArchiveListView', selector:'messageArchiveListView'},
{ref:'sysDeptTreeView', selector:'sysDeptTreeView'},
{ref:'messageArchiveButton', selector:'messageArchiveButton'},
{ref:'messageArchiveContainer', selector:'messageArchiveContainer'},
{ref:'messageArchiveAddMain', selector:'messageArchiveAddMain'},
{ref:'messageArchiveRelationMain', selector:'messageArchiveRelationMain'},
{ref:'messageArchiveBaseAll', selector:'messageArchiveBaseAll'},
{ref:'messageArchiveBaseInfo', selector:'messageArchiveBaseInfo'},
{ref:'messageBaseSearchWithDpetView', selector:'messageBaseSearchWithDpetView'}],
init : function( container ) {
var panel = Ext.widget('messageArchiveContainer');
container.add(panel);
var archiveStore = this.getMessageArchiveListView().store;
archiveStore.reload();//自动加载表单数据,如果默认为空,可以直接注释该行。
this.control({
'sysDeptTreeView' : {// 绑定部门树的单击事件
itemclick : this.loadArchiveByTree
},
'messageArchiveListView button[action=add]' : {// 绑定新增按钮
click : this.addArchive
},
// 'messageArchiveListView button[action=view]' : {// 绑定查看按钮
// click : this.viewArchive
// },
'messageArchiveListView button[action=delete]' : {// 绑定删除按钮
click : this.deleteArchive
},
'messageArchiveAdd button[action=save]' : {// 绑定新增按钮
click : this.doAddArchive
},
'messageArchiveListView button[action=search]' : {// 绑定查询按钮
click : this.searchArchive
},
'messageArchiveBaseInfo button[action=save]' : {// 绑定新增保存按钮
click : this.doAddArchive
}
});
},
// 绑定部门树的单击事件
loadArchiveByTree : function(view, rec, item, index, eventObj) {
//获取form
var form = this.getMessageBaseSearchWithDpetView().form;
//重置form
form.reset();
//给部门ID
var field = form.findField("deptPk");
//赋值
field.setValue(rec.data.id);
var archiveStore = this.getMessageArchiveListView().store;
archiveStore.load({
params : {
deptPk : rec.data.id
}
})
},
viewArchive : function(btn) {
alert(1);
},
deleteArchive : function(btn) {
var grid = btn.up("grid");
var rows = grid.getSelectionModel().getSelection();
if (rows == null || rows.length < 1) {
Ext.MessageBox.alert("提示", "请选择一行!");
return false;
}
var arr = new Array();
for (var i = 0; i < rows.length; i++) {
var row = rows[i].data;
arr[i] = row.pkId;
}
Ext.Msg.confirm('提示', '确定要删除?', function(btn) {
if (btn == "yes") {
Ext.Ajax.request({
url : base + "/com/correct/archive/delete.html",
params : {
'ids' : arr
},
method : 'post',
success : function(response, options) {
// var respText = eval("("+response.responseText+")");
var respText = Ext.decode(response.responseText);
Ext.MessageBox.alert('成功', respText.message);
grid.store.reload();
},
failure : function(response, options) {
Ext.MessageBox.alert('失败',
response.responseText.message);
}
});
} else {
}
});
},
// 绑定新增按钮
addArchive : function(btn) {
var win = Ext.widget("messageArchiveAddMain");
win.show();
},
// 绑定新增保存按钮
doAddArchive : function(btn) {
var win = btn.up("window");
var form = win.down("form");
form.form.doAction('submit', {
url : base + "/com/correct/archive/archiveAllAdd.html",
method : 'post',
success : function(form, action) {
Ext.Msg.alert('操作成功', action.result.message);
win.close();
// 刷新store
var grid = this.getMessageArchiveListView();
grid.store.reload();
},
failure : function(form, action) {
if (action.result) {
Ext.Msg.alert('操作失败', action.result.message);
} else {
Ext.Msg.alert('操作失败', "请填写,有**标识的字段!");
}
}
});
},
// 绑定查询按钮
searchArchive : function(btn) {
var form = btn.up("form");
console.log(form);
var params = form.getValues();
var grid = this.getMessageArchiveListView();
grid.store.reload({
params : params
});
}
});