Rob*_*uch 1 javascript model-view-controller ember.js
主要思想:在我的控制器成功更新模型的属性后,我想调用一个关闭子视图的视图方法.
模板:
<a {{action showUpdate href=true target="view"}}>update</a>
{{#if view.isUpdate}}
{{#view App.UpdateView}}
...here is a form
<button {{action update}}>Send</button>
{{/view}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)
视图:
App.MainView = Ember.View.extend({
templateName: 'update',
isUpdate: false,
showUpdate: function() {
this.set('isUpdate', !this.get('isUpdate'));
}
});
App.UpdateView= Ember.View.extend();
Run Code Online (Sandbox Code Playgroud)
控制器:
App.MainController = Ember.Controller.extend({
updateCon: function() {
do something
},
...
Run Code Online (Sandbox Code Playgroud)
路由器:
update: function(router, evt) {
router.get('mainController').updateCon();
//router.get('mainController.view').showUpdate(); <-- doesnt work
}
Run Code Online (Sandbox Code Playgroud)
所以我试着在我的控制器运行良好之后在我的路由器中调用它,但它不起作用.
Cannot call method 'showUpdate' of null
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?什么时候:我错过了什么?当没有:当我的控制器做了什么时,如何改变我的观点?
控制流逻辑更适合控制器.保持视图不受状态通常是一个好主意,控制器不应该知道视图.您可以将更新标志和功能移动到控制器,并将控制器作为Handlebars模板中的目标.视图的Handlebars模板类似于:
<a href="#" {{action showUpdate target="controller"}}>update</a>
{{#if isUpdated}}
...
{{/if}}
Run Code Online (Sandbox Code Playgroud)
然后控制器将具有更新逻辑:
App.MainController = Em.Controller.extend({
isUpdated: false,
showUpdate: function() {
this.set('isUpdated', !this.get('isUpdated'));
}
});
Run Code Online (Sandbox Code Playgroud)
视图现在只是模板名称:
App.MainView = Em.View.extend({
templateName: 'update'
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2913 次 |
| 最近记录: |