0
本質上,我有一個對象列表,用戶可以通過在文本框中鍵入來添加新對象。隨着用戶輸入,出現新的對象。但是,當用戶單擊保存時,該對象將從列表中消失。它已成功保存到數據庫(Ruby on Rails/SQLite),因此它顯示爲全面刷新。它爲什麼這樣做,以及如何在用戶按下提交時讓它保持在屏幕上?對象從保存在Ember.js中的視圖中消失
下面是一些相關的代碼:
index.hbs
<div class="messages">
{{#each userhashtag in controller}}
{{render "userhashtag" userhashtag}}
{{/each}}
{{#link-to 'userhashtags.new'}} Add Hashtag{{/link-to}}
</div>
{{outlet}}
show.hbs
{{userhashtag.name}} <button class="btn btn-dancer" type="submit" {{action "destroy"}}>Remove</button><br>
new.hbs
<form role="form">
<fieldset>
<div class="form-group">
{{view Ember.TextField valueBinding='name' class='form-control' name='name' viewName='nameField'}}
</div>
<div class="btn-group">
<button type="submit" {{action "save"}} class="btn btn-success">Submit</button>
</div>
路線
App.UserhashtagsNewRoute = Ember.Route.extend({
model: function(){
return this.store.createRecord('userhashtag');
},
setupController: function(controller, model){
controller.set('model', model);
},
renderTemplate: function() {
this.render('app/templates/userhashtags/new');
},
actions: {
save: function(){
return this.controller.get('model').save().then(function(){
this.transitionTo('userhashtags');
}.bind(this));
}
}
});