基本上我有一个对象列表,用户可以通过键入文本框来添加新对象。当用户键入时,将出现新对象。但是,当用户单击“保存”时,该对象将从列表中消失。它已成功保存到数据库(Ruby on Rails / SQLite),因此它会在完全刷新时显示。为什么这样做,当用户按下提交时如何让它留在屏幕上?
以下是一些相关代码:
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));
}
}
});
答案 0 :(得分:0)
确保您手动将新模型推送到Array集合中(不确定如何管理集合)。我建议将#save动作移动到控制器中,在那里管理对象列表。
// assuming "list" is some reference to your collection list within your controller
save: function() {
this.get('userhashtag').save().then(function(_model){
list.pushObject(_model);