Forms 如何管理新创建的对象而不使用;“储蓄”;在转换到新路线之前,是否在emberjs中进行?
我有一个问题,我有一个新路线的资源。当我转换到新路线时,我创建了一个新对象。在窗体上,我有一个按钮要取消,它将删除该对象。但是,如果我在导航上单击一个链接,比如说返回到资源索引,那么该对象就是我在表单中输入的任何内容。管理创建对象然后离开表单的最佳方法是什么 我的路线:Forms 如何管理新创建的对象而不使用;“储蓄”;在转换到新路线之前,是否在emberjs中进行?,forms,ember.js,persistence,Forms,Ember.js,Persistence,我有一个问题,我有一个新路线的资源。当我转换到新路线时,我创建了一个新对象。在窗体上,我有一个按钮要取消,它将删除该对象。但是,如果我在导航上单击一个链接,比如说返回到资源索引,那么该对象就是我在表单中输入的任何内容。管理创建对象然后离开表单的最佳方法是什么 我的路线: App.Router.map(function() { this.resource('recipes', function() { this.route('new'); this.route('show',
App.Router.map(function() {
this.resource('recipes', function() {
this.route('new');
this.route('show', { path: '/:recipe_id' });
});
this.resource('styles');
});
App.RecipesNewRoute = Ember.Route.extend({
model: function() {
return App.Recipe.createRecord({
title: '',
description: '',
instructions: ''
});
},
setupController: function(controller, model) {
controller.set('styles', App.Style.find());
controller.set('content', model);
}
});
新路线的我的控制器:
App.RecipesNewController = Ember.ObjectController.extend({
create: function() {
this.content.validate()
if(this.content.get('isValid')) {
this.transitionToRoute('recipes.show', this.content);
}
},
cancel: function() {
this.content.deleteRecord();
this.transitionToRoute('recipes.index');
},
buttonTitle: 'Add Recipe'
});
我使用的是1.0.0.rc.1版
谢谢 您在路线的
deactivate
方法中放置的任何代码都将在每次离开该路线时执行。如果用户没有明确保存新模型,以下代码将删除该模型
App.RecipesNewRoute = Ember.Route.extend({
// ...
deactivate: function() {
var controller = this.controllerFor('recipes.new');
var content = controller.get('content');
if (content && content.get('isNew') && !content.get('isSaving'))
content.deleteRecord();
},
// ...
});
另外,当用户按下“取消”按钮时,您现在不需要显式删除记录。谢谢,这非常有效。这里有一个指向文档的链接,以防有人好奇。