getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

Error when side loading nested resource #9

Closed dsawardekar closed 11 years ago

dsawardekar commented 11 years ago

Thanks for releasing Epf. It looks very promising.

I am having a problem with side loading comments for a posts model. I am using the library compiled from master with build-browser.

The /posts requests goes out and returns a 200, but the route throws the following exception.

Uncaught TypeError: Cannot read property 'length' of undefined epf.js:990
  Ep.Model.reopenClass.inverseFor epf.js:990
  Ep.HasManyArray.Ep.ModelArray.extend.arrayContentWillChange epf.js:1188
  superWrapper ember.js:1079
  Ember.ArrayProxy.Ember.Object.extend.arrangedContentArrayWillChange ember.js:12322
  sendEvent ember.js:2096
  Ember.Array.Ember.Mixin.create.arrayContentWillChange ember.js:9622
  Ember.Mixin.create.replace ember.js:12749
  Ember.ArrayProxy.Ember.Object.extend.replaceContent ember.js:12115
  Ep.HasManyArray.Ep.ModelArray.extend.replaceContent epf.js:1180
  superWrapper ember.js:1079
  Ember.ArrayProxy.Ember.Object.extend._replace ember.js:12231
  Ember.ArrayProxy.Ember.Object.extend._insertAt ember.js:12245
  Ember.ArrayProxy.Ember.Object.extend.pushObject ember.js:12289
  superWrapper ember.js:1079
  Ember.MutableArray.Ember.Mixin.create.addObject ember.js:10343
  (anonymous function) ember.js:10004
  Ember.EnumerableUtils.forEach ember.js:1562
  Ember.MutableEnumerable.Ember.Mixin.create.addObjects ember.js:10004
  Ep.Serializer.Ember.Object.extend.deserializeLazyHasMany epf.js:500
  Ep.Serializer.Ember.Object.extend.deserializeHasMany epf.js:487
  (anonymous function) epf.js:476
  (anonymous function) epf.js:1094
  (anonymous function) ember.js:2888
  OrderedSet.forEach ember.js:2731
  Map.forEach ember.js:2886
  Ep.Model.reopenClass.eachRelationship epf.js:1093
  Ep.Model.reopen.eachRelationship epf.js:1105
  Ep.Serializer.Ember.Object.extend.deserializeRelationships epf.js:474
  Ep.Serializer.Ember.Object.extend.deserializeModel epf.js:453
  Ep.JsonSerializer.Ep.Serializer.extend.deserialize epf.js:242
  superWrapper ember.js:1079
  Ep.RestAdapter.Ep.Adapter.extend.processData epf.js:2058
  Ep.RestAdapter.Ep.Adapter.extend.didReceiveDataForFind epf.js:2047
  Backburner.run ember.js:4623
  Ember.run ember.js:5112
  (anonymous function) epf.js:1988
  invokeCallback ember.js:7096
  (anonymous function) ember.js:7139
  EventTarget.trigger ember.js:6927
  (anonymous function) ember.js:7194
  DeferredActionQueues.flush ember.js:4901
  Backburner.end ember.js:4591
  (anonymous function) ember.js:4796

My models are,

App.Post = Ep.Model.extend
  title: Ep.attr('string')
  body: Ep.attr('string')

  comments: Ep.hasMany('App.Comment')

App.Comment = Ep.Model.extend
  author: Ep.attr('string')
  body: Ep.attr('string')

  post: Ep.belongsTo('App.Post')

And the model hook on the PostsRoute is,

App.PostsRoute = Ember.Route.extend
  model: () ->
    return this.session.query('post')

My server-side implementation is with active_model_serializers.

class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :body
  has_many :comments, embed: :ids, include: true
end

which gives the following json for /posts.

{
    "comments": [
        {
            "id": 1,
            "author": "me01",
            "body": "comment-body"
        }
    ],
    "posts": [
        {
            "id": 2,
            "title": "lorem04",
            "body": "post-body",
            "comment_ids": [
                1
            ]
        }
    ]
}

Is there anything wrong with my json output. Can you please post sample json that Epf expects for this scenario.

Thanks.

jasonkriss commented 11 years ago

I haven't tried this out myself, but could this be related to #4? You are defining relationships via strings but this doesn't seem to be supported quite yet. Do you still have the problem if you change your models to:

App.Post = Ep.Model.extend({
  title: Ep.attr('string'),
  body: Ep.attr('string')
});
App.Comment = Ep.Model.extend
  author: Ep.attr('string',)
  body: Ep.attr('string'),

  post: Ep.belongsTo(App.Post)
});
App.Post.reopen({
  comments: Ep.hasMany(App.Comment)
});
dsawardekar commented 11 years ago

Thanks @jasonkriss. I must have glossed over this part in the docs. Sideloading works fine after doing the comments assignment in reopen.

jasonkriss commented 11 years ago

No problem. Glad to hear that took care of it for now.

ghempton commented 11 years ago

Yeah I think #4 will fix this. I will try and get this feature in soon. Closing this issue and we can track there.