【问题标题】:Newly added child record not shown in parent view in emberjs(only shown after refreshing the page)emberjs的父视图中不显示新添加的子记录(仅在刷新页面后显示)
【发布时间】:2013-12-30 07:19:12
【问题描述】:

我正在将子记录添加到父记录。我在这里面临的问题是,每当我向“表单”模型添加新的“项目”时,新添加的项目不会显示在显示所有项目列表的编辑器模板中。只有当我刷新页面时,我才能看到新添加的“项目”记录。谁能告诉我这是为什么?这种变化甚至反映在 ember-data 中,我可以在其中看到新添加的记录“项目” 这些是我的模型协会

    Eidos.Form = DS.Model.extend({
      name: DS.attr('string'),
      items: DS.hasMany('item',{async:true})

    });

子模型是

      Eidos.Item = DS.Model.extend({
      qtitle: DS.attr('string'),
      qhelp: DS.attr('string')
    )};

这是我为每个 form_id 编辑表单并将项目添加到此模型对象的编辑器路由模型钩子。

model: function(){
    return this.modelFor('form');
}

这是我添加子记录的控制器操作

    additem: function(form){
        var title = this.get('qtitle');
        var help_text = this.get('qhelp');
        console.log(title,help_text);
        // var items = form.get('items');
        var item = this.store.createRecord('item', {
            qtitle: title,
            qhelp: help_text,
            form: form
        });
        item.save().then(function(){
            console.log('item successfully created');
                    });

        this.set('qtitle','');
        this.set('qhelp','');
        this.transitionToRoute('editor');

    }

我的编辑器模板是

    {{#each item in items}}
        <br><strong>{{item.qtitle}}</strong>
        <strong> {{item.qhelp}}</strong>
    {{/each}}
    <form {{action 'additem' this on='submit'}}
    <strong> Question title: {{input type='text' placeholder='enter the question title' size='40' valueBinding='qtitle'}} <hr>
    Help text: {{input type='text' placeholder='enter the help text' size='60' valueBinding='qhelp'}}
    <button class='btn btn-success'> Done adding Item </button>

一切正常,我能够创建与 form_id 关联的项目记录。 我面临的唯一问题是,编辑器模板没有显示最近添加的子记录。每当我在控制器中使用“additem”操作添加新的子记录时。 谁能帮我解决这个问题?

【问题讨论】:

    标签: ruby-on-rails ember.js ember-data handlebars.js


    【解决方案1】:

    其实很简单,只需要通过'pushObject'方法将新创建的子记录推送到父记录,在handlebars模板中体现变化即可。

    http://railscasts.com/episodes/408-ember-part-1?view=asciicast

    我已经知道了,但是犯了一些愚蠢的语法错误。

    这是正确的代码

            item.save().then(function(){
                console.log('item successfully created');
            });
            this.get('items').pushObject(item);
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 2018-04-26
      • 2016-12-24
      相关资源
      最近更新 更多