2013-11-24 33 views
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)); 
    } 
} 
}); 

回答

0

确保您手动推新机型到您的数组集合(不知道你是如何管理的集合)。我建议将#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);