- EmberJS 教程
- EmberJS - 主頁
- EmberJS - 概述
- EmberJS - 安裝
- EmberJS - 核心概念
- 建立和執行應用程式
- EmberJS - 物件模型
- EmberJS - 路由
- EmberJS - 模板
- EmberJS - 元件
- EmberJS - 模型
- EmberJS - 管理依賴
- EmberJS - 應用程式關注點
- EmberJS - 配置 Ember.js
- EmberJS - Ember Inspector
- EmberJS 有用資源
- EmberJS - 快速指南
- EmberJS - 有用資源
- EmberJS - 討論
EmberJS - 建立和刪除記錄
你可以在模型的例項上建立和刪除記錄。
語法
import Ember from 'ember';
export default Ember.Route.extend ({
model() {
//code here
},
actions:{
addNewCategory(id, name) {
this.controller.get('model').pushObject({ var1,va2});
},
deleteCategory(category) {
this.controller.get('model').removeObject(model_name);
}
}
});
示例
下面給出的示例展示了記錄的建立和刪除。使用名稱 *record_demo* 建立新的路由,並在該路由中建立另一個路由,並將其命名為 *categories*。現在開啟 *router.js* 檔案,用於定義 URL 對映 -
import Ember from 'ember';
//Access to Ember.js library as variable Ember
import config from './config/environment';
//It provides access to app's configuration data as variable config
//The const declares read only variable
const Router = Ember.Router.extend ({
location: config.locationType,
rootURL: config.rootURL
});
//Defines URL mappings that takes parameter as an object to create the routes
Router.map(function() {
this.route('record_demo', function() {
this.route('categories');
});
});
//It specifies Router variable available to other parts of the app
export default Router;
使用以下程式碼開啟創建於 *app/templates/* 中的 *application.hbs* 檔案 -
{{#link-to 'record_demo'}}Go to Records demo page{{/link-to}}
{{outlet}}
當你單擊上面的連結時,它將開啟創建於 *app/templates/* 下的 *record_demo* 模板頁面。*record_demo.hbs* 檔案包含下列程式碼 -
<h2>Welcome...Click the below link for Categories page</h2>
{{#link-to 'record_demo.categories'}}Go to Categories page{{/link-to}}
{{outlet}}
上述模板頁面將開啟創建於 *app/templates/record_demo* 下的 *categories.hbs* 檔案,其中包含以下程式碼 -
<h2>Categories Page</h2>
<form>
<label>ID:</label>
{{input value=newCategoryId}}
<label>NAME:</label>
{{input value = newCategoryName}}
//when user adds records, the 'addNewCategory' function fires and adds
the records to model
<button type = "submit" {{action 'addNewCategory' newCategoryId newCategoryName}}>
Add to list
</button>
</form>
<ul>
{{#each model as |category|}}
<li>
Id: {{category.id}}, Name: {{category.name}}
//when user delete records, the ‘deleteCategory’ function fires and remove
the records from model
<button {{action 'deleteCategory' category}}>Delete</button>
</li>
{{/each}}
</ul>
//it counts the number of added records and removed records from the model
<strong>Category Counter: {{model.length}}</strong>
{{outlet}}
現在開啟創建於 app/routes/record_demo 中的 categories.js 檔案,其中包含以下程式碼 -
import Ember from 'ember';
export default Ember.Route.extend ({
model() {
//model will display these records when you execute the code
return [{
id: 1,
name: 'Category One'
}, {
id: 2,
name: 'Category Two'
}];
},
actions: {
//it adds records to model
addNewCategory(id, name) {
this.controller.get('model').pushObject({id,name});
},
//it removes the records from model
deleteCategory(category) {
this.controller.get('model').removeObject(category);
}
}
});
輸出
執行 ember 伺服器;你會收到以下輸出 -
當你單擊連結時,它將開啟包含類別頁面連結的 records_demo 頁面 -
接下來,將開啟類別模板頁面。在輸入框中輸入 id 和名稱並單擊 *新增到列表* 按鈕,如下面的螢幕截圖所示 -
接下來,單擊新增按鈕;你將看到列表中新增的記錄,並且計數將遞增 -
如果你想從列表中刪除記錄,那麼單擊 *刪除* 按鈕。
emberjs_model.htm
廣告