0
votes

So I am moving our app from ember-data 0.13 to ember-data 1.0 and I seem to be having trouble with 'sideloading' when I want to find all/multiple Records of a type and creating a computed property out of it. I have a model like this:

App.Event = DS.Model.extend({
    someAttributes    : DS.attr('string'),
    rel       : DS.hasMany('rel'),

    important: function () {
        var rel = this.get('rel');
        return rel.filterProperty('reactionType', 'important');
    }.property('rel'),

    whatever: function () {
        var rel = this.get('rel');
        return rel.filterProperty('reactionType', 'whatever');
    }.property('rel')
})

And the JSON response from the server looks like this:

{events:
    [{ someAttributes: 'attr1', rels: [1, 2, 4]}, 
    {someAttributes: 'attr2', rels: [3]}],
 rels:
    [{ id: 1, reactionType: "whatever" },
    { id: 2, reactionType: "important" },
    { id: 3, reactionType: "important" },
    { id: 4, reactionType: "whatever"}]
 }

So first of all, is that JSON response correct? (Haven't seen an official example yet) And second of all how can I get a computed property like this to work? It worked in ember-data 0.13. I also tried to move the computed property to the 'rel' model, but that doesn't seem to have any effect. Anyway, hope that makes sense and appreciate any help.

1

1 Answers

1
votes

You can try this. For hasMany, you need to have plural: rels: DS.hasMany('rel') . And you need @each. JSON format is correct.

App.Event = DS.Model.extend({
    someAttributes    : DS.attr('string'),
    rels       : DS.hasMany('rel'),

    important: function () {
        var rel = this.get('rels.content');
        return rel.filterProperty('reactionType', 'important');
    }.property('rels.@each'),

    whatever: function () {
        var rel = this.get('rels.content');
        return rel.filterProperty('reactionType', 'whatever');
    }.property('rels.@each')
}) 

Update: modified this.get('rels') to this.get('rels.content')