Ember - 如何在嵌套数据对象上使用pushObject?

时间:2013-08-07 23:39:02

标签: ember.js

由于这篇文章How to create nested models in Ember.js?

,我已经可以将新对象推送到jobs和jobProducts数组中了

但我似乎无法推动新的分配或交付。我在下面包含了JSON对象。

任何建议都表示赞赏,当我得到片刻时,我会把我现在所处的位置放在一起。

干杯

App.jobs = [
  {
    id: 0,
    jobTitle: "This is the only job",
    jobProducts: [
      {
        id: 0,
        productTitle: "Product 1",
        allocations:[
          {
            id: 0,
            allocationTitle: "Allocation 1",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 1"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 2"
              }
            ]
          },
          {
            id: 1,
            allocationTitle: "Allocation 2",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 3"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 4"
              }
            ]
          }
        ]
      },
      {
        id: 1,
        productTitle: "Product 2",
        allocations:[
          {
            id: 0,
            allocationTitle: "Allocation 3",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 5"
              },
             {
               id: 1,
               deliveryTitle: "Delivery 6"
             }
           ]
          },
          {
            id: 1,
            allocationTitle: "Allocation 4",
            deliverys:[
              {
                id: 0,
                deliveryTitle: "Delivery 7"
              },
              {
                id: 1,
                deliveryTitle: "Delivery 8"
              }
            ]
          }
        ]
      }
    ]
  }
];

1 个答案:

答案 0 :(得分:3)

简短:

以下是您可以执行此操作的示例:http://jsbin.com/esixeh/7/edit

长:

在示例中,您将找到如下所示的代码行,这些代码行看起来很可怕,但它可以工作:

App.get('jobs').objectAt(0).jobProducts.objectAt(0).allocations.objectAt(0).deliverys.pushObject({...});

从你的JSON结构开始,从App.get('jobs')开始,对象只是简单的javascript对象而不是从Ember.Object延伸,你不能使用像.get('allocations').get('deliverys')这样的ember方法在他们身上并将他们连在一起,如:

App.get('jobs').get('jobProducts').get('allocations').get('deliverys');

App.get('jobs.jobProducts.allocations.deliverys');

但你仍然可以使用像.allocations这样的普通javascript点符号访问器。

在数组上你仍然可以使用ember的.pushObject().objectAt()等而不是普通的.push(),因为默认情况下see here框架会扩充数组,以获取更多信息。这一点。

希望它有所帮助。