Ember - 如何在嵌套数据对象上使用pushObject? [英] Ember - how to use pushObject on nested data object?

查看:108
本文介绍了Ember - 如何在嵌套数据对象上使用pushObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以把一个新的对象推入job和jobProducts数组,感谢这篇文章如何在Ember.js中创建嵌套模型?



但我似乎不能推动新的分配或交付。我已经在下面包含了JSON对象。



任何建议都表示赞赏,当我得到一点时,我会把我现在所在的小提琴放在一起。



干杯

  App.jobs = [
{
id :0,
jobTitle:这是唯一的工作,
jobProducts:[
{
id:0,
productTitle:Product 1,
分配:[
{
id:0,
allocationTitle:分配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,
allocationments:[
{
id:0,
allocationTitle:Allocation 3,
deliverys:[
{
id:0,
deliveryTitle:Delivery 5
}
{
id:1,
deliveryTitle:Delivery 6
}
]
},
{
id:1,
allocationTitle:分配4,
deliverys:[
{
id:0,
deliveryTitle:Delivery 7
} ,
{
id:1,
deliveryTitle:Delivery 8
}
]
}
]
}
]
}
];


解决方案

短: / p>

这是一个例子,您可以如何做: http ://jsbin.com/esixeh/7/edit



长:



在这个例子中,你会发现如下所示的代码行,看起来很可怕,但是它的作用是:

 应用程序获得( '作业')objectAt(0).jobProducts.objectAt(0).allocations.objectAt(0).deliverys.pushObject({...})。 

由于您的JSON结构,从 App.get('jobs' )对象只是简单的javascript对象,不会从 Ember.Object ,你不能使用ember方法,如 .get('allocations') .get('deliverys'),并将它们链接在一起,如:

 应用程序。得到( '工作')得到( 'jobProducts')得到( '分配')得到( 'deliverys')。; 

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

但您仍然可以使用简单的JavaScript点符号访问器,如 .allocations 。您可以使用ember的 .pushObject() .objectAt() 等,而不是简单的 .push(),因为默认情况下,框架会增加数组的大小(请参阅这里了解更多信息。



希望它帮助。


I can already push a new object into the jobs and jobProducts array thanks to this post How to create nested models in Ember.js?

but I cannot seem to push new allocations or deliverys. I have included the JSON object below.

Any advice is appreciated, I will put together a fiddle of where I currently am when I get a moment.

Cheers

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"
              }
            ]
          }
        ]
      }
    ]
  }
];

解决方案

The short:

here is an example how you might do it: http://jsbin.com/esixeh/7/edit

The long:

In the example you will find code lines like the below, which look scary but it works:

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

Since down you JSON structure, starting from App.get('jobs') the objects are just plain javascript objects and do not extend from Ember.Object you cannot use ember methods like .get('allocations') or .get('deliverys') on them and chain them together like:

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

or

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

but you can still use plain javascript dot notation accessor like .allocations.

On array's you can still use ember's .pushObject(), .objectAt() etc. instead of plain .push(), because array's are augmented by the framework by default see here for more info on that.

Hope it helps.

这篇关于Ember - 如何在嵌套数据对象上使用pushObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆