【问题标题】:Add and remove items from the customer’s item-list using ember.js relationships使用 ember.js 关系从客户的项目列表中添加和删除项目
【发布时间】:2013-11-30 00:49:03
【问题描述】:

我正在使用 ember.js 构建我的第一个应用程序并且遇到了这个问题:

客户应该能够在列表中添加和删除items。执行此操作的标准方法是创建一个新 item 并将其添加到 customer

App.Customer = DS.Model.extend(
{
    firstname: DS.attr('string'),
    lastname: DS.attr('string'),
    items: DS.hasMany('item')
});

App.Item = DS.Model.extend(
{
    customer: DS.belongsTo('customer'),
    description: DS.attr('string')
});

我的问题是我不能施展魔法并让一个项目出现,我必须从我的项目池中选择一个,将它分配给客户,当从客户那里移除项目时,我必须把它放回我的项目池。

如何在不破坏任何对象本身的情况下删除对象之间的关系?这意味着我想让外键像在 MySQL 数据库中那样跳跃。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您可以像这样从客户那里删除商品:

    在 CustomerEditController 中:

    removeItem: function(item) {
        this.get('items').removeObject(item);
    }
    

    然后在您的客户编辑模板中:

    {{#each items}}
        {{this.description}}<button {{action removeItem this}}>remove item</button>
    {{/each}}
    

    【讨论】:

      【解决方案2】:

      @Steve H。这不是我的意思,但多亏了你的帮助,我自己才能弄清楚。

      我必须在您的 removeItem 函数中添加几行

      removeItem: function (item)
      {
          this.get('store').find('customer', 0).then(function (myPool)
          {
              this.get('items').removeObject(item);
              myPool.get('items').pushObject(item);
          });
      }
      

      【讨论】:

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