associationsbatman.js

Nested models not created as batman objects


Given the following models with one-to-many relationship:

class App.Post extends Batman.Model
  @hasMany 'comments'

class App.Comment extends Batman.Model
  @belongsTo 'post'

My comments are included in the JSON of the post from the backend. Since i'm having @encode 'comments' the comments are added to the post. However, they are added in an array of simple JS objects instead of an associationset of Batman objects.

Should I really decode them explicitly like this

@encode 'comments',
  decode: (value, key, incomingJSON, outgoingAttributes, record) ->
    outgoingAttributes = App.Comment.createMultipleFromJSON(incomingJSON.comments)

or am I doing something stupid here?


Solution

  • @hasMany "comments" should automatically set up an encoder to load comments from JSON.

    Did you mention that you added your own encoder, like

    @encode 'comments'
    

    ?

    If so, that is overriding the one created by @hasMany 'comments'. Try removing the @encode 'comments'. Does that help?