我有一個燼應用程序,它具有帶CRUD功能的users
模型。 Create
和Edit
模板都以twitter引導模式呈現。我的問題是,當我點擊模式中的Cancel
或Save Changes
時,我希望該應用返回到users
模板。相反,當我點擊其中任何一個按鈕時,模式消失,但url分別保留在/ users/create或/ users/edit中。我試圖改變路由如下,但它沒有效果。我的模式是大量使用Google搜索並將不同示例中的代碼放在一起的結果,因此我不確定userEditView
中的close
操作實際上是否執行了任何操作。誰能幫忙?灰燼模式路由到錯誤的網址關閉
App.UserEditView = Ember.View.extend({
templateName: "user/edit",
title: "",
content: "",
classNames: ["modal", "fade"],
didInsertElement: function() {
this.$().modal("show");
this.$().one("hidden", this._viewDidHide);
},
// modal dismissed by example clicked in X, make sure the modal view is destroyed
_viewDidHide: function() {
if (!this.isDestroyed) {
this.destroy();
}
},
// here we click in close button so _viewDidHide is called
close: function() {
this.$(".close").click();
//This is where I've tried to change the routing
this.transitionToRoute('users');
}
});
我的模板:
<script type = "text/x-handlebars" id = "user/edit">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Edit User</h4>
</div>
<div class="modal-body">
<div class = "input-group">
<h5>First Name</h5>
{{input value=firstName}}
<h5>Last Name</h5>
{{input value=lastName}}
<h5>Email</h5>
{{input value=email}}
</div>
</div>
<div class="modal-footer">
<a {{action close target="view"}} href="#" class="btn btn-default">Cancel</a>
<a {{action "save"}} class="btn btn-primary">Save changes</a>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</script>
我的控制器:
App.UsersCreateController = Ember.ObjectController.extend({
actions: {
save: function(){
console.log("this.get('model')", this.get('model'));
// create a record and save it to the store
var newUser = this.store.createRecord('user', this.get('model'));
console.log("newUser", newUser);
newUser.save();
// redirects to the user itself
this.transitionToRoute('user', newUser);
}
}
});
我的路線:
App.UsersCreateRoute = Ember.Route.extend({
model: function(){
// the model for this route is a new empty Ember.Object
return Em.Object.create({});
},
renderTemplate: function(){
this.render('user.create', {
controller: 'usersCreate'
});
}
});
感謝偉大的jsbin!當我點擊取消時,這個功能完美,但是當我保存一條新記錄時,模式關閉,但頁面仍然淡出,網址仍然位於/ users/newuserid。有什麼建議麼? – bookthief
@bookthief很高興幫助!看起來沒問題,當我選擇'save'時,會顯示兩個警告對話框,並轉換爲用戶(以chrome 31.x,ff 25.0進行測試)。 – melc
嗯我在某個地方犯了一個錯誤,因爲視圖中的警報沒有被觸發! – bookthief