带有hasMany的Ember store.push不更新模板? [英] Ember store.push with hasMany doesn't update template?

查看:58
本文介绍了带有hasMany的Ember store.push不更新模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下模型

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

路线 session_route.js.coffee

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

以下 session.hbs 模板:

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

我已连接到WebSocket流,当一个新的SessionUser被创建时,我会收到通知。

这里 sessions_controller.js.coffee 来测试推送一些有效载荷:

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)

使用Ember Inspector(Chrome扩展程序)我可以看到会话用户被推送并且存在于具有正确关系的商店中,但模板未更新。

当使用 store.createRecord 时,模板实际上正在更新,但我想使用push / pushPayload以便我可以使用现有的序列化器。

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.

推荐答案

我很确定这是一个已知的bug。我们使用websocket执行类似操作:在我们按有效负载创建记录后,我们手动调用 addObject 将其添加到hasMany关系中,例如

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

这篇关于带有hasMany的Ember store.push不更新模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆