Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload an async with links hasMany relationship?

Lets say we have

Post = DS.Model.extend({
  comments: DS.hasMany({async: true})
})

Comment = DS.Model.extend({
  post: DS.belongsTo()
})

and I have to use the links feature because otherwise I get a 414 error due to too much comments on a post.

Since this commit https://github.com/emberjs/data/commit/4d717bff53f8c93bedbe74e7965a4f439882e259

It seems like it's impossible to trigger a reload of the post.get('comments'), ie sending a GET request on for example post/42/comments.

Is there any solution for that ?

like image 305
sly7_7 Avatar asked Nov 14 '13 16:11

sly7_7


1 Answers

UPDATE

Like @sly7_7 said in your answer below, this feature is now avaliable in ember-data. So if you are using the 1.0.0-beta.11 version or greater, this code isn't needed

You can reopen the ManyArray, and create a new method called reloadLinks(). With the following:

var get = Ember.get;

DS.ManyArray.reopen({
    reloadLinks: function() {
        var records = get(this, 'content'),
        store = get(this, 'store'),
        owner = get(this, 'owner'),
        type = get(this, 'type'),
        name = get(this, 'name'),
        resolver = Ember.RSVP.defer();

        var meta = owner.constructor.metaForProperty(name);
        meta.type = type;
        var link = owner._data.links[meta.key];
        store.findHasMany(owner, link, meta, resolver);
    }
});

The usage is the following:

comments.reloadLinks();

Give a look in that fiddle http://jsfiddle.net/H6Gqf/

like image 138
Marcio Junior Avatar answered Sep 18 '22 09:09

Marcio Junior