7
votes

I have been reading the Ember documentation for v2.4, and I came across a part that I don't understand:

Some models may have properties that are deeply nested objects of readonly data. The naïve solution would be to define models for each nested object and use hasMany and belongsTo to recreate the nested relationship. However, since readonly data will never need to be updated and saved this often results in the creation of a great deal of code for very little benefit. An alternate approach is to define these relationships using an attribute with no transform (DS.attr()). This makes it easy to access readonly values in computed properties and templates without the overhead of defining extraneous models.

In my use case, I will only be displaying data, it will never be saved back to the server. Every example I have found for nested data, shows setting up separate models for each level of nesting, then setting up the relationships, as referred to as the "naive solution". So what would be the correct way to go about this? Can anyone please expand on this for me? Thanks in advance!

1
Where does this data from from? If it isn't persisted to the server, it isn't a DS.attr(), it's just a regular hash.locks
This cleared things up for me: thejsguy.com/2016/01/29/…Alex Aloia
@AlexAloia You might consider posting your findings as an answer to your own question, for others to benefit from, if they run into the same situation. Your question has been up-voted 7 times, as of my comment. See here for How to Write a Good Answer.jacefarm

1 Answers

0
votes

Looking at the comments, the answer would be:

Define the root object and define the fields of that object as DS.attr(). With no type information.

You will still be able to access the nested data using dot notation but you will not need to specify any more of the structure.

Have a look at the following link for a more complete look at this topic.

https://thejsguy.com/2016/01/29/working-with-nested-data-in-ember-data-models.html