How to reload an async with links hasMany relationship?

前端 未结 6 1308
有刺的猬
有刺的猬 2021-01-02 01:34

Lets say we have

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

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

a

6条回答
  •  盖世英雄少女心
    2021-01-02 02:14

    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/

提交回复
热议问题