Ember-Data: How to use `DS.Adapter.findHasMany`

前端 未结 4 1889
栀梦
栀梦 2021-02-06 01:36

UPDATE

Note that this question applies to Ember Data pre-1.0 beta, the mechanism for loading relationships via URL has changed significantly post-1.0 beta!


4条回答
  •  旧巷少年郎
    2021-02-06 02:15

    Here is my solution but it is on Ember-data 0.14, so the world has moved on, even if we are still on this code base:

      findHasMany: function(store, record, relationship, details) {
        if(relationship.key !== 'activities') {
          return;
        }
    
        var type = relationship.type,
            root = this.rootForType(type),
            url = this.url + details.url,
            self = this;
    
        this.ajax(url, "GET", {
          data: {page: 1}
        }).then(function(json) {
          var data = record.get('data'),
            ids = [],
            references = json[relationship.key];
    
          ids = references.map(function(ref){
            return ref.id;
          });
    
          data[relationship.key] = ids;
    
          record.set('data', data);
    
          self.didFindMany(store, type, json);
          record.suspendRelationshipObservers(function() {
            record.hasManyDidChange(relationship.key);
          });
        }).then(null, DS.rejectionHandler);
      },
    

    I found replacing the data with the ids worked for me.

提交回复
热议问题