【问题标题】:Object disappearing from the view on save in Ember.js在 Ember.js 中保存时对象从视图中消失
【发布时间】:2013-11-24 04:39:50
【问题描述】:

基本上我有一个对象列表,用户可以通过在文本框中键入来添加新对象。当用户键入时,新对象出现。但是,当用户单击保存时,该对象将从列表中消失。它已成功保存到数据库(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>

新的.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));
    }
}
});

【问题讨论】:

    标签: javascript ruby-on-rails ember.js


    【解决方案1】:

    确保您手动将新模型推送到您的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      相关资源
      最近更新 更多