Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember - how to use pushObject on nested data object?

Tags:

ember.js

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"
              }
            ]
          }
        ]
      }
    ]
  }
];
like image 472
scotta7exander Avatar asked Mar 18 '26 02:03

scotta7exander


1 Answers

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.

like image 58
intuitivepixel Avatar answered Mar 22 '26 00:03

intuitivepixel