1

I have an nested data structure as follows:

Job:{  
     JobId: 1,  
     NumberTrackr: 2,  
     Trackrs: [  
                1 : { TaskTrackrID: a, 
                      NumberSlots:1, 
                      slots: [
                             slot1: {uniqueId:foo, you: get, the:[point, by, now]}
                              ]
                    },
                2 : { TaskTrackrID: b, 
                      NumberSlots:1, 
                      slots: [
                             slot1: {uniqueId:bar, you: get, the:[point, by, now]}
                              ]
                    }
            ]
} 

And my application uses this data hierarchy (calculating the sum of a specific Trackr's 'children's' foo attribute) as well as each data level as a whole (for example, calculating statistics from the content of all slots regardless of their 'parent' TaskTracker).

Im new to Ember, but my I was thinking of creating a model for each object level (TaskTrackr, Slot etc). This model would consist of all the attributes shown above as in addition to some sort of array of sub models (using arraycontroller?). Said submodels would also have attributes as well as their own array of sub models. It is important that the highe level objects can have computed properties calculated from their sub objects. It is also important that I can access each level as a whole and modify specific attributes.

What would the code look like that would enable me to access this data both as a tree as well as on a level basis?

Thank you so much for your help

1 Answer 1

2

Take a look at Ember Data. It supports these types of relationships.

App.Job = DS.Model.extend({
  trackers: DS.hasMany('App.Tracker')
})

App.Tracker = DS.Model.extend({
  job: DS.belongsTo('App.Job');
})

// ... and so on

var job = App.Job.createRecord();
job.get('trackers').pushObject(App.Tracker.createRecord());

Just to note. You mentioned using ArrayController and you will likely use that inside of your Ember application. However, when modeling your data/relationships you will not use any controllers. You should be able to do do what you pasted above with using only DS.Model

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I see that your code sets up the hierarchy, but how would you access all Trackers?
For a job: job.get('trackers'). For all trackers in the world: App.store.findAll(App.Tracker). Read the Ember Data github page, lots of info there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.