2

Assuming the following models:

App.User = DS.Model.extend(
  email: DS.attr('string')
  session_users: DS.hasMany('sessionUser')
)

App.SessionUser = DS.Model.extend(
  user: DS.belongsTo('user')
  state: DS.attr('string')
  session: DS.belongsTo('session')
)

App.Session = DS.Model.extend(
  title: DS.attr('string')
  session_users: DS.hasMany('sessionUser')
)

The route session_route.js.coffee:

App.SessionRoute = Ember.Route.extend(
  model: (params) ->
    this.store.find('session', params.id)
)

And the following session.hbs template:

{{#each session_users}}
    {{state}}
{{/each}}

I'm connected to a WebSocket stream, when a new SessionUser is created, I get notified.
Here sessions_controller.js.coffee to test pushing some payload:

payload =
    id: 20
    user: controller.store.getById('user', 2)
    session: controller.store.getById('session', 2)
    state: 'confirmed'
  controller.store.push('sessionUser', payload)

Using Ember Inspector(Chrome Extension) I can see the session user was pushed and is exists in the store with the right relationships, but the template wasn't updated.
When using store.createRecord the template is actually getting updated, but I want to use push/pushPayload so that I can use the existing serializers.

Adir
  • 1,423
  • 3
  • 19
  • 32
  • Your route looks like it's only finding a single session, but you are iterating over an array. – Kingpin2k Nov 01 '13 at 20:34
  • n/m, I see your session_users is a variable under the session. – Kingpin2k Nov 01 '13 at 20:35
  • If you call find right after the push does it load it? – Kingpin2k Nov 01 '13 at 20:35
  • the find function returns me the pushed sessionUser, but the template is still the same, why only createRecord affects the template? – Adir Nov 01 '13 at 20:44
  • if you use just the ids in the payload for user and session is it the same? – Kingpin2k Nov 01 '13 at 22:38
  • It seems that calling session.addRecord with the returned model of push, does update the template. Looks like the model is getting pushed but Ember doesn't add it to the ManyArray, is it a bug? – Adir Nov 02 '13 at 10:09

1 Answers1

5

I am pretty sure this is a known bug. We do something similar with a websocket: after we push the payload to create the record, we manually call addObject to add it to the hasMany relationship e.g.

var comment = store.push('comment', payload);
post.get('comments').addObject(comment);
gerry3
  • 21,420
  • 9
  • 66
  • 74