本文介绍了DS.FixtureAdapter失去了具有多个异步属性的fixture数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提交了一个与Ember数据团队,但我希望了解如何解决这个问题(或者如果我一直是错误的)

I've already submitted a github issue with the Ember Data team, but I'd love insight into how to work around this (or if I was mistaken all along)

如果此错误

jsfiddle的问题在这里:
此处重复此代码以进行更清晰的解释。

jsfiddle of the issue here: http://jsfiddle.net/cdownie/JxhBv/1/Code is duplicated here for a clearer explanation.

我有以下数据模型,我想用灯具测试:

I have the following data model that I'd like to test with fixtures:

App.Parent = DS.Model.extend({
    name: DS.attr('name'),
    kids: DS.hasMany('kids', {async : true})
});
App.Kid = DS.Model.extend({
    name: DS.attr('name'),
    grade: DS.attr('string')
});

我正在使用以下灯具来测试:

I'm using the following fixtures to test it:

App.Parent.FIXTURES = [
    {id: 'doe', name: 'john', links: {kids: '/fake/url/for/kids'}}
];
App.Kid.FIXTURES = [
    {id: 'doe-jr', name: 'john jr', grade: '4th'}
];

现在,DS.FixtureAdapter默认不支持这种关系,所以我不得不写我自己的扩展名填写了 findHasMany 方法。在这个实现中,我使用父ID作为所有孩子的前缀。

Now, DS.FixtureAdapter by default doesn't support this kind of relationship, so I had to write my own extension that filled out the findHasMany method. In this implementation, I use the parent ID as a prefix for all the children.

App.CustomFixtureAdapter = DS.FixtureAdapter.extend({
    defaultSerializer: '_umrest',

    findHasMany: function(store, record, link, relationship) {
        var type = relationship.type;
        var parentId = record.get('id');
        return store.findAll(relationship.type).then(function(children) {
            var content = children.get('content');
            var filteredContent = content.filter(function(child) {
                return (child.get('id').indexOf(parentId) == 0);
            });

            // The children we return here are fully resolved with data.
            var kid = filteredContent[0];
            console.log('The findHasMany method is returning a kid with id      :', kid.get('id'),  ' name: ', kid.get('name'), ' grade:', kid.get('grade'));

            return Ember.RSVP.resolve(filteredContent);
        });
    },

    queryFixtures: function(fixtures, query, type) {
        return fixtures;
    }
});



错误



Inside findHasMany ,我的 App.Kid 实体已完全解决。它的所有数据都在那里。但是,在请求父母的任何其他代码中, App.Kid 型号具有ID,但没有其他数据。在我的演示中,这在索引路线中演示:

The bug

Inside findHasMany, my App.Kid entity is fully resolved. All its data is there. However, in any other code that requests a parent's kid, the App.Kid model has an ID but no other data. In my demo, this is demonstrated in the index route:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        // Return the first kid of the first parent (in our data: the only kid)
        return this.store.findAll('parent').then(function(parentRecords) {
            var parent = parentRecords.get('content')[0];
            return parent.get('kids').then(function(kids) {
                return kids.get('content')[0];
            });
        });
    },
    setupController: function(controller, model) {
        var id = model.get('id'), 
            name = model.get('name'), 
            grade = model.get('grade');

        console.log('The model in setupController is returning a kid with id:', id,  ' name: ', name, ' grade:', grade);

        console.log('Is that model fully loaded?', model.get('isLoaded') ? 'yes': 'no');

        controller.set('id', id);
        controller.set('name', name);
        controller.set('grade', grade);
    }
});



预期行为



自从我们得到从$ store.findAll(U.Kid)调用自定义fixture适配器中的$ code> App.Kid 我希望在通过hasMany关系获得该模型时,将完全解决模型。

Expected behavior

Since we get a fully-resolved App.Kid model from a store.findAll(U.Kid) call in the custom fixture adapter, I'd expect that fully resolved model to be present when I get that model through the hasMany relationship.

在js小提琴中:

DEBUG: Ember      : 1.6.1
DEBUG: Ember Data : 1.0.0-beta.8.2a68c63a
DEBUG: Handlebars : 1.0.0
DEBUG: jQuery     : 1.8.3

在另一个环境中:

DEBUG: Ember      : 1.5.1
DEBUG: Ember Data : 1.0.0-beta.8.2a68c63a
DEBUG: Handlebars : 1.0.0
DEBUG: jQuery     : 1.9.1


推荐答案

我想出来了。问题在于我执行 findHasMany 。系统期望一系列香草数据对象,如:

I figured it out. The problem is in my implementation of findHasMany. The system expects an array of vanilla data objects, like:

[{id: 'doe-jr', name: 'john jr', grade: '4th'}]

当我实际返回的是一个 App.Kid 模型。

when what I was actually returning was an array of App.Kid models.

修改方法如下:

findHasMany: function(store, record, link, relationship) {
    var type = relationship.type;
    var parentId = record.get('id');
    return store.findAll(relationship.type).then(function(children) {
        var content = children.get('content');
        var filteredContent = content.filter(function(child) {
            return (child.get('id').indexOf(parentId) == 0);
        });
        // Fixed the issue by pulling out the raw data from the fixture model 
        // and returning that.
        var data = filteredContent.map(function(content) {
            return content.get('data');
        });

        return Ember.RSVP.resolve(data);
        // Switch that return statement to the following to show original bug:
        // return Ember.RSVP.resolve(filteredContent);
    });
},

这篇关于DS.FixtureAdapter失去了具有多个异步属性的fixture数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:29