强制ember-data从服务器重新加载特定的记录 [英] Force ember-data to re-load specific record from server

查看:165
本文介绍了强制ember-data从服务器重新加载特定的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有两个帖子的嵌套资源。在路由器中定义如下。

  this.resource('posts',function(){
this .resource('post',{path:'/:post_id'});
});

每篇文章也可以有多个相关评论。



在我的* posts_route.js *中,我设置了这样的模型:

  return this.get('store ').findAll(' 后'); 

这使得我的后台api发出GET请求,该API返回所有帖子为json。在这一点上,我只需要基本的信息(没有评论),所以并不是所有的数据都包含在返回的json中。后来,如果我点击一个特定的帖子,我在我的* post_route.js *中执行此操作:

  return this.get store')。find('post',params.post_id); 

有了这个我希望为这个特定的帖子做一个新的GET请求。但是,ember数据不会执行新的服务器请求。我在寻找它的本地商店寻找它,但不是我希望获得的扩展数据。



有没有办法强制ember数据对服务器做一个真正的请求?谢谢。

解决方案

您可以使用 model.reload()强制一个新的GET请求到展示路线。您可以在显示路线的 afterModel hook或 setupController 钩子中执行此操作。如果您真的不想再次触发此请求,如果模型已经加载,您可以在模型上定义一个纯旧的重新加载的属性:

  MyModel = DS.Model.extend({
...属性...
重新加载:false
})

然后将其设置为 true 当重载完成时:

  model.reload()。then(function(response){model.set('reloaded' ,true');}); 


Lets say I have 2 nested resources for posts. It is defined like this in my router.

this.resource('posts', function () {
    this.resource('post', {path: '/:post_id'});
});

Each post can then also have multiple related comments.

In my *posts_route.js* i set up the model like this:

return this.get('store').findAll('post');

This makes a GET request to my backend api which returns all posts as json. At this point I only need basic information (and no comments), so not all the data is included in the returned json. Later, if I click on a specific post I do this in my *post_route.js*:

return this.get('store').find('post', params.post_id);

With this I wish to do a new GET request for only that specific post. But ember-data does not do a new server request. I looks for it in the local store where it finds it, but not with the extended data I was hoping to get.

Is there a way to force ember-data to do a real request to the server? Thanks.

解决方案

You can use model.reload() to force a new GET request to the show route. You could do this in the afterModel hook or the setupController hook of your show route. If you really don't want to trigger this request again if the model has already been loaded, you can just define a plain old reloaded property on your model:

MyModel = DS.Model.extend({
   ... attributes ...
   reloaded: false
})

And then set that to true when the reload completes:

model.reload().then(function(response) { model.set('reloaded', true'); });

这篇关于强制ember-data从服务器重新加载特定的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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