Javascript 模型中的更改不受视图余烬的影响

Javascript 模型中的更改不受视图余烬的影响,javascript,ember.js,Javascript,Ember.js,我尝试像这样手动向模型添加数据 beforeModel: function() { var scope =this; Ember.$.getJSON('/restURL').then(function(response){ scope.store.pushPayload('consultation',response); }, 数据加载成功后,我可以在ember调试器中看到它,但我有一个问题-数据没有呈现在视图上 application.

我尝试像这样手动向模型添加数据

beforeModel: function() {
    var scope =this;
    Ember.$.getJSON('/restURL').then(function(response){
                    scope.store.pushPayload('consultation',response);
},
数据加载成功后,我可以在ember调试器中看到它,但我有一个问题-数据没有呈现在视图上

application.hbs中的模板:

{{#each item in model}}
           {{#link-to 'consultation' item}}{{item.remoteUser.name}}{{/link-to}}
{{/each}}

注意:当我使用
this.store.find('consultation')加载数据时
工作正常,但我有自定义URL,无法使用此结构。

您应该使用
afterModel
挂钩,而不是
beforeModel
,因为
beforeModel
不用于数据聚合
beforeModel
发生在模型尝试解析之前,它无法访问解析的模型,因此您不能依赖它向模型添加额外数据

另一方面,
afterModel
hook将解析后的模型作为第一个参数传入,因此您可以根据需要进一步装饰模型,并且可以像
model
hook一样返回承诺


看看这个简单的例子:

据我所知,您希望使用直接ajax调用加载协商。按照您现在的方式,协商将在
beforeModel
中检索,然后,由于您没有返回承诺,因此在ajax调用完成之前,Ember会立即执行
model
钩子。模型钩子中的
this.store.find
可能会向服务器发出另一个可能无效的请求。最简单的方法就是

model: function() {
  var store = this.store;

  return Ember.$.getJSON('/restURL')
    .then(function(response) {
      store.pushPayload('consultation', response);
      return store.all('consultation');
    });
}
请注意使用了
store.all
,它是存储中已存在的该类型的所有对象的动态集合

您也可以考虑将逻辑分解为<代码>前面的< <代码> > <代码>型号<代码>,如:

beforeModel: function() {
  return Ember.$.getJSON('/restURL')
    // this binding style is a matter of personal preference :-)
    .then(this.store.pushPayload.bind(this.store, 'consultation'))
},

model: function() {
  return this.store.all('consultation');
}

那么你的
模型
钩子是什么呢?模型钩子包含:返回这个.store.find('consultation');在beforeModel中,我想在我的案例中添加额外的字段resolvedModel没有push和pushPayload方法。我收到消息:UncaughtTypeError:Object[Object Object]没有方法“pushPayload”,谢谢你,torazaburo。这是工作。我将这个钩子添加到我的应用程序路径中。我还有记录模型,在通过Ajax通过URL加载咨询后,我需要选择这个咨询并显示它的记录。我需要在咨询路线中添加一些钩子吗?