Ber*_*nni 4 ember.js ember-router
我正在使用Ember.JS编写一个CRUD应用程序:
我想使用相同的模板来显示/编辑现有的模型对象并创建一个新的模型对象.
这是我使用的路由器代码.
App = Ember.Application.create();
App.Router.map(function() {
this.resource('actions', {path: "/actions"}, function() {
this.resource('action', {path: '/:action_id'});
this.route('new', {path: "/new"});
});
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('actions');
}
});
App.ActionsIndexRoute = Ember.Route.extend({
model: function () {
return App.Action.find();
}
});
App.ActionRoute = Ember.Route.extend({
events: {
submitSave: function () {
this.get("store").commit();
}
}
});
App.ActionsNewRoute = Ember.Route.extend({
renderTemplate: function () {
this.render('action');
},
model: function() {
var action = this.get('store').createRecord(App.Action);
return action;
},
events: {
submitSave: function () {
this.get("store").commit();
}
}
});
Run Code Online (Sandbox Code Playgroud)
问题是,当我第一次显示一个动作,然后再回来创建一个动作时,看起来模板没有使用新创建的记录,而是使用之前显示的记录.
我的解释是控制器和模板不同步.你会怎么做?也许有一种更简单的方法来实现这一目标?
这是一个带有代码的JSBin:http://jsbin.com/owiwak/10/edit
通过说this.render('action'),你不只是告诉它使用action模板,而且ActionController实际上你想要action模板,但是ActionNewController.
你需要覆盖:
this.render('action', {
controller: 'actions.new'
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
653 次 |
| 最近记录: |