HasMany's ManyArray


#1

What is the difference between these two usages of ManyArray? Is one more correct than the other? Does it matter?

//app/models/post.js
export default DS.Model.extend({
  comments: DS.hasMany('comment')
});

//app/models/comment.js
export default DS.Model.extend({
  post: DS.belongsTo('post')
});


// Usage 1: Access a post's comments directly
post.get('comments.length');
post.get('comments').forEach((comment) => {
    // Do something with comment here
});
post.get('comments').pushObject(pushThisComment);
post.get('comments').removeAt(removeTheCommentAtThisIndex);


// Usage 2: Access a post's comments after promise resolves
post.get('comments').then((comments) => {
    comments.get('length');
    comments.forEach((comment) => {
        // Do something with comment here
    });
    comments.pushObject(pushThisComment);
    comments.removeAt(removeTheCommentAtThisIndex);
});

#2

Yes it does matter. Here’s a link to check out: https://guides.emberjs.com/v2.6.0/models/relationships/#toc_many-to-many In the first usage your program cannot continue until this process finish. The second usage causes the program to work in the background, gathering all the data first, letting other processes continue. When the second has received all data it resumes.(keeping it’s promise to return a value.)


#3

So both methods yield the exact same result, and the only difference is that the first usage is synchronous while the second is asynchronous?