Javascript 如何在显示视图之前使Ember.js加载重新调整数据?

Javascript 如何在显示视图之前使Ember.js加载重新调整数据?,javascript,rest,ember.js,Javascript,Rest,Ember.js,我有一个带有RestAdapter的应用程序,可以从服务器获取正确的数据: App.AFile= DS.Model.extend({ name: DS.attr( 'string' ), ... App.Store = DS.Store.extend({ revision: 13, adapter: DS.RESTAdapter.extend({url: "myapi"}) }); 还有一张这样的地图: App.Router.map(function() { this.res

我有一个带有RestAdapter的应用程序,可以从服务器获取正确的数据:

App.AFile= DS.Model.extend({
  name: DS.attr( 'string' ),
...

App.Store = DS.Store.extend({
  revision: 13,
  adapter: DS.RESTAdapter.extend({url: "myapi"})
});
还有一张这样的地图:

App.Router.map(function() {
  this.resource('allfiles', { path: '/allfiles' });
  this.resource('onefile', {path: '/onefile/:onefile_id' });
App.allfilesRoute = Ember.Route.extend({
  model: function()
  {
      return App.AFile.find();
  }
});

App.onefileRoute = Ember.Route.extend({
  model : function(params)
  {
      return App.AFile.find(params.onefile_id);
  }
});
路线定义如下:

App.Router.map(function() {
  this.resource('allfiles', { path: '/allfiles' });
  this.resource('onefile', {path: '/onefile/:onefile_id' });
App.allfilesRoute = Ember.Route.extend({
  model: function()
  {
      return App.AFile.find();
  }
});

App.onefileRoute = Ember.Route.extend({
  model : function(params)
  {
      return App.AFile.find(params.onefile_id);
  }
});
这些模板:

 <script type="text/x-handlebars" data-template-name="allfiles">
 {{#each controller}}
    {{#linkTo onefile this}}open{{/linkTo}}
 {{/each}}
 </script>

 <script type="text/x-handlebars" data-template-name="onefile">
   {{name}}
 </script>
它的工作原理是这样的:用户打开应用程序,它显示所有文件模板,并带有一个名为“打开”的链接。该链接打开名为onefile的模板,并将onefile\u id传递给该模板

当我打开应用程序并单击“打开”时,它会工作并显示一个文件的正确名称。URL被设置为/onefile/1,其中1是onefile\u id。因此它可以正常工作

但当我刷新page/onefile/1时,将不再显示名称

在返回id之前,我已经检查了onefileRoute模型函数中发生的情况,发现App.AFile中定义的所有字段的值都为空。应用程序加载后,这些值会正确填充到app.AFile对象中,但不会显示在视图中

所以看起来RestAdapter在视图显示之后获取数据

如何让它工作

App.onefileRoute = Ember.Route.extend({
  model : function(params)
  {
      var m = App.AFile.find(params.onefile_id);
      // at this point m might not be resolved, so name could be empty
      return m;
  },
  afterModel: function(model, transition){
      // at this point it should be resolved, what happens here?
      console.log(model.get('name'));
  }
});

我猜您的端点返回了错误的id模型信息。

在aftermodel模型中。get'name'在Chrome中仍然返回Null,如果您查看网络选项卡,是否看到呼叫和响应?