简体   繁体   中英

Ember data EmbeddedRecordMixin

I've been trying to make an embedded list of models get loaded. I understood from the demo that EmbeddedRecordsMixin was the way to go but this still fails with: "Error: Assertion Failed: TypeError: factory is undefined" I have tried to separate them in my fixtures and this works just fine so I must be missing something in the embedding part even though it follows this: http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html

Does this doesn't work with Fixtures then?

var App = window.App = Ember.Application.create({
  LOG_TRANSITIONS: true
});
var attr = DS.attr;

App.Modificators = DS.Model.extend({
    "tpe": attr('string')
});
App.SpecialStuff = DS.Model.extend({
    "title": attr('string'),
    "body": attr('string'),
    "modificators": DS.hasMany('modificators')
});

App.SpecialStuffSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        "modificators": { embedded: 'always' }
    }
});

App.SpecialStuff.reopenClass({
    FIXTURES: [{
        "id": 79,
        "title": "fewfew",
        "body": "kkk",
        "modificators": [{
            "id": 1,
            "tpe": "vv",
        },
        {
            "id": 2,
            "tpe": "mv",
        }]
    }]
});
App.SpecialStuffIndexRoute = Ember.Route.extend({
  model: function (params) {
    return this.store.find('special_stuff');
  }
});

App.Router.map(function () {
  // Add your routes here
  this.resource('specialStuff', function() {});
});

Ember.Inflector.inflector.uncountable('modificators');
Ember.Inflector.inflector.uncountable('special_stuff');
App.ApplicationAdapter = DS.FixtureAdapter.extend({});

Ember Data's Fixture Adapter doesn't use a serializer for fetching data. You're better off mocking json calls with something like https://github.com/jakerella/jquery-mockjax and using the rest adapter.

Here's some examples: Ember-data embedded records current state?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM