【问题标题】:Saving parent_id relationship with ember data使用 ember 数据保存 parent_id 关系
【发布时间】:2013-08-27 04:10:36
【问题描述】:

好的。我已经通过 Google 返回的 SO 和其他来源进行了很好的查看,但还没有找到我的问题的答案。

我有两个模型:

App.Kid = Ember.Model.extend
    title: DS.attr "string"
    parent: DS.belongsTo "App.Parent"

App.Parent = Ember.Model.extend
    title: DS.attr "string"
    kids: DS.hasMany "App.Kid"

这里的大多数问题都讨论了从侧面加载的 JSON 数据中检索 ember 数据关系,这是我能够做到的。我需要知道的是如何在创建新孩子时保存 parent_id?

目前我正在 App.KidController 中保存一个新孩子,如下所示:

App.ParentController = Ember.ObjectController.extend
    needs: [ "widgets" ]

App.KidCreateController = Ember.ObjectController.extend
    needs: [ "dashboard" ]

    saveChild: ->
        @content.get( "store" ).commit()

保存时看不到 parent_id,但是我确实得到了一个新的父对象(分配给父键),其 id 为 0。

谁能解释我在这里做错了什么?

更新 1

所以我会用我的实际情况来解释我在做什么。

一个用户有多个仪表板,这些仪表板附加了多个小部件。我的模型结构如下所示:

App.Dashboard = DS.Model.extend
    title: DS.attr "string"
    widgets: DS.hasMany "App.Widget"

App.Widget = DS.Model.extend
    title: DS.attr "string"
    type:  DS.attr "string"
    ...
    dashboard: DS.belongsTo "App.Dashboard"

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    首先要注意,您已用ember-data 标记您的问题,但您使用Ember.Model.extend 定义模型,这是在使用ember-model 作为持久性库时使用的,因此您应该将其更改为DS.Model.extend 或以不同的方式标记您的问题,我假设在我的回答中ember-data

    如果您的App.Kid 模型属于具有kids 数组的App.Parent,那么您应该使用ParentController 来保存属于该父级的新子代,这样您就可以访问父级@987654331 @ 是 kids 数组,您需要将新的 Childs 推入其中。

    示例:

    给定一个像这样的简单模板:

    {{#each parent in model itemController="parent"}}
       <strong>(id: {{parent.id}}) {{parent.title}}</strong>
       <div class="well">
         <strong>Kids</strong>
         <ul>
         {{#each kid in parent.kids}}
           <li>(id: {{kid.id}}) {{kid.title}}</li>
         {{/each}}
         </ul>
         Kid name: {{input valueBinding="newKidName"}} <button class="btn btn-info" {{action saveChild newKidName}}>Add kid</button>
       </div>
    {{/each}}
    

    然后您应该为每个父项定义一个ParentController 并在那里创建一个saveChild 函数:

    App.ParentController = Ember.ObjectController.extend({
      needs: ['dashboard'],
      saveChild: function(newKidName) {
       // get the dashboard id you want with
       // this.get('controllers.dashboard').get('id');
       // this assumes you have one dashboard?
    
       // create the new child
        var newKid = App.Kid.createRecord({title: newKidName});
        // push the newly created child into the parent's kids array
        this.get('content').get('kids').pushObject(newKid);
        // commit changes to the parent
        this.get('content').get('store').commit();
      }
    });
    

    有关示例的简单演示,请参见此处:http://jsbin.com/odosoy/115/edit

    希望对你有帮助。

    【讨论】:

    • 好吧,我肯定更接近了。但是我仍然无法获取父 ID。如果我按照你演示的那样做,并在创建新记录时在父键下添加 ID,我会得到:Assertion failed: You can only add a record of App.Parent to this relationship 你能想到我可能做错了什么吗?
    • @Brayniverse,编辑了我的答案,我添加了needs,这样您就可以访问您的仪表板控制器,我还明确删除了设置父级,它应该可以在没有的情况下工作
    • 抱歉,我对从我们最初的父子示例的转换感到有些困惑。您能否将仪表板的使用更改为原始名称?
    • 我不应该避免混淆。我想要实现的是,一个用户有多个仪表板,每个仪表板都有几个小部件。
    • @Brayniverse,对不起,我完全弄错了:(你的问题中只有needs,没有模型定义我没有得到你真正想要的......如果你能提供一个你的设置的更多代码我再看看
    【解决方案2】:

    好吧,原来我是个白痴。我使用 Laravel 作为后端,在定义模型时,您需要定义哪些属性是可填充的,但是我忘记将 dashboard_id 添加到列表中。现在一切正常。

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多