我正在使用Ember数据和RESTAdapter以及Django的扩展.
这是一个JSBin
以下是我们的路线设置方式:
App.Router.map(function() { this.resource('locations'); this.resource('location', {path:'locations/:location_id'}, function() { this.resource('items', function() { this.route('create'); }); this.resource('item', { path:'item/:item_id' }, function() { this.route('edit'); }); }); }); App.LocationsRoute = Ember.Route.extend({ model: function () { return this.get('store').find('location'); } }); App.ItemsRoute = Ember.Route.extend({ model: function(){ //Get the model for the selected location and grab its item list. //This will do a call to /locations/1/items return this.modelFor('location').get('items'); } });
现在,当我们导航到位置/ 1 /项目时,这一切都正常.向用户呈现与id为1的位置相关的项目列表.
当用户单击其中一个项目时,它会将URL带到#/ locations/1/item/1,并显示ID为1的项目的详细信息.
现在什么不起作用是这样的:
当我按下后退按钮时,#/ locations/1/items路由加载但它不再拥有其数据,并且不会发生对api/locations/1/items的REST调用.即使我们第一次导航到#/ locations/1/items时数据显示也很好.
就像Ember说的那样"我们已经加载了这些数据,所以我们不需要再次调用api"但是数据在某种程度上没有显示在我们的模板中.
如果我将ItemsRoute模型更改为:
return this.get('store').find('item');
上面的场景非常好,但数据不是基于我们的位置.
使用this.modelFor有什么我缺少的东西吗?或者我的路线设置不正确?
如果您需要任何额外信息,请告诉我.
更新1:
将模型功能更改为此后,我发现了一些新的见解:
model: function(){ //Get the model for the selected location and grab its item list. //This will do a call to /locations/1/items var location = this.modelFor('location'); var items = location.get('items'); return items; }
在第一次加载#/ locations/1/items时,location变量保存位置模型,而items变量包含具有'content'属性,'isFulfilled:true'和其他一些内容的东西.
这正确地工作并显示项目列表.当我点击某个特定项目并进入#/ locations/1/items/1然后点击后退按钮.断点触发器和位置仍使用位置模型正确填充.
但是items变量似乎只是一个PromiseArray,我需要以某种方式等待承诺在这个模型钩子返回之前完成吗?我以为Ember已经自动完成了吗?我想这意味着路径在履行承诺之前加载,这就是为什么没有显示数据?
因此我的列表没有正确填充.