Ember store.push with hasMany doesn't update template?

佐手、 提交于 2019-12-22 14:18:56

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/19733900/ember-store-push-with-hasmany-doesnt-update-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!