標籤:call app 作用 strong font this 通過 window 建立
原文連結:EXT JS MVC開發模式
在app(亦即根目錄)檔案夾下面建立controller、model、store和view檔案夾,從名稱上就知道他們該放置什麼代碼了吧。然後建立Application.js作為我們程式的入口檔案,並在mvc.html中引用Application.js檔案。
目錄結構
-->app(根目錄)
------>controller
------>model
------>store
------>view
------>application.js(與controller等同級目錄)
建立model(實體類)
在model檔案夾下面,建立user.js檔案
Ext.define(‘MyApp.model.User‘, {
extend: ‘Ext.data.Model‘,
fields: [
{ name: ‘name‘, type: ‘string‘ },
{ name: ‘age‘, type: ‘int‘ },
{ name: ‘phone‘, type: ‘string‘ }
]
});
建立store
雖然store不是mvc中必須的步驟,但是作為資料倉儲,store起到了資料存取的作用,grid、form等展現的資料都是通過store來提供的,因此store在extjs mvc開發模式中是必不可少的
//app/store/user.js 的代碼如下
Ext.define("MyApp.store.User", {
extend: "Ext.data.Store",
model: "MyApp.model.User",
data: [
{ name: "Tom", age: 5, phone: "123456" },
{ name: "Jerry", age: 3, phone: "654321" }
]
});
建立view
為了實現列表和編輯功能,我們需要兩個視圖,分別是list和edit,那麼view的結構如下:
app/view/user/List.js 對應我們的grid的定義,只不過將建立grid的執行個體改成了建立grid的擴充。
//app/view/user/List.js 代碼如下:
Ext.define("MyApp.view.user.List", {
extend: "Ext.grid.Panel",
alias: ‘widget.userlist‘,
store: "User",
initComponent: function () {
this.columns = [
{ text: ‘姓名‘, dataIndex: ‘name‘ },
{ text: ‘年齡‘, dataIndex: ‘age‘, xtype: ‘numbercolumn‘, format: ‘0‘ },
{ text: ‘電話‘, dataIndex: ‘phone‘ }
];
this.callParent(arguments);
}
});
//app/view/user/edit.js 代碼如下:
Ext.define("MyApp.view.user.Edit", {
extend: "Ext.window.Window",
alias: "widget.useredit",
title: "編輯使用者",
width: 300,
height: 200,
layout: "fit",
items: {
xtype: "form",
margin: 5,
border: false,
fieldDefaults: {
labelAlign: ‘left‘,
labelWidth: 60
},
items: [
{ xtype: "textfield", name: "name", fieldLabel: "姓名" },
{ xtype: "numberfield", name: "age", fieldLabel: "年齡" },
{ xtype: "textfield", name: "phone", fieldLabel: "電話" }
]
},
buttons: [
{ text: "儲存", action: "save" }
]
});
主入口
Ext.application({
name: "MyApp",
appFolder: ‘app‘,
controllers: ["User"],
autoCreateViewport: true,
launch: function () {
// 頁面載入完成之後執行
}
});
說明:
● name: 應用程式名稱
● appFolder:應用程式代碼的目錄,用來進行動態載入代碼的
● controllers: 應用程式使用到的控制器
● autoCreateViewport:是否自動建立Viewport,預設為false,我們這裡設定為true,當它被設定為true的時候,應用程式會自動建立Viewport,這個Viewport的定義在我們的app/view/viewport.js 中;如果為false的時候,我們需要在launch中手動建立相應的視圖。
【轉】EXT JS MVC開發模式