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

前端 未结 4 1931
栀梦
栀梦 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:24

    I was glad to find this post, helped me. Here is my version, based off the current ember-data and your code.

    findHasMany: function(store, record, relationship, details) {
        var adapter = this;
        var serializer = this.get('serializer');
        var type = relationship.type;
        var root = this.rootForType(type);
        var url = (typeof(details) == 'string' || details instanceof String) ? details : this.buildURL(root);
        return this.ajax(url, "GET", {}).then(function(json) {
                adapter.didFindMany(store, type, json);
                var list = $.map(json[relationship.key], function(o){ return serializer.extractId(type, o);});
                store.loadHasMany(record, relationship.key, list);
            }).then(null, $.rejectionHandler);
    },
    

    for the reload issue, I did this, based on code I found in another spot, inside the serializer I overrode:

    materializeHasMany: function(name, record, hash, relationship) {
        var type = record.constructor,
                key = this._keyForHasMany(type, relationship.key),
                cache = record.cacheFor('data');
        if(cache) {
            var hasMany = cache.hasMany[relationship.key];
            if (typeof(hasMany) == 'object' || hasMany instanceof Object) {
                record.materializeHasMany(name, hasMany);
                return;
            }
        }
        var value = this.extractHasMany(type, hash, key);
        record.materializeHasMany(name, value);
    }
    

    I'm still working on figuring out paging, since some of the collections I'm working with need it.

提交回复
热议问题