Ember.js 强制从服务器加载ember data store.find
如果服务器eaven已经存储了余烬数据,那么有没有一种好方法可以强制它从服务器eaven加载该资源 我有一个简单的show user操作,它执行Ember.js 强制从服务器加载ember data store.find,ember.js,ember-data,Ember.js,Ember Data,如果服务器eaven已经存储了余烬数据,那么有没有一种好方法可以强制它从服务器eaven加载该资源 我有一个简单的show user操作,它执行store.find('users',id)第一次尝试显示页面时只加载一次模型,第二次尝试显示页面时,我的模型从存储中加载,这是我知道的正常余烬数据行为。但是我每次都需要加载它 编辑: 我找到的唯一方法就是这样做: @store.find('user',{id: params.user_id}).then (users)-> users.get
store.find('users',id)
第一次尝试显示页面时只加载一次模型,第二次尝试显示页面时,我的模型从存储中加载,这是我知道的正常余烬数据行为。但是我每次都需要加载它
编辑:
我找到的唯一方法就是这样做:
@store.find('user',{id: params.user_id}).then (users)->
users.get('firstObject')
然而,它迫使我在索引操作上执行“假”显示操作 我认为
祝您好运此外,您可以调用
getById
,它将返回该记录存在的任何实例或null,然后调用unloadRecord
将其从缓存中删除。不过我也喜欢埃杜的回答,这样我就不用担心其他地方的记录了。也许我会使用getById
然后reload
,这样任何引用用户的引用都会得到更新。(请原谅我的错误咖啡脚本,如果它是错误的)
多亏了:
本周末,作为查询参数功能的一部分,有一个新方法将被添加到测试版中,名为Route.refresh()
您可以在setupcontrollerhook中使用promise和Edu提到的重载方法来完成这项工作
setupController: ->
@store.find('myModel', 1).then (myModel) ->
myModel.reload()
如果您确定要显示的记录在某个操作后将发生更改,则可以在路由中调用
this.refresh()
方法。例如:
ProductsRoute = Ember.Route.extend
model: ->
@store.find 'product',
activated: true
actions:
accept: (product) ->
if not product.get('activated')
product.set 'activated', true
product.save()
.catch (err) ->
console.log err
product.rollback()
.then =>
@refresh()
ignore: (product) ->
if not product.get('ignored')
product.set 'ignored', true
product.save()
.catch (err) ->
console.log err
product.rollback()
.then =>
@refresh()
如果从子路线调用操作-例如产品/建议-将为父路线和子路线重新加载模型。我认为您正在寻找的是嗨。我在setupController中将模型设置为:this.set('model',this.store.find('product',model.id));我如何在这里使用reload()?这种方法的一个不幸的副作用是,如果您从项目列表中转换,则该项目将在页面重新呈现之前从列表中消失,因为它将从存储中删除。我也没有尊重用户是承诺的事实,因此总是会调用unloadRecord。只有当它是一个余烬数据
过滤器(过滤器,全部,findAll
,仅查找类型)时,它才会从列表中消失。实际上,你的文本说的是getById
,这是我认为你要放在代码段中的,不是?
/**
Refresh the model on this route and any child routes, firing the
`beforeModel`, `model`, and `afterModel` hooks in a similar fashion
to how routes are entered when transitioning in from other route.
The current route params (e.g. `article_id`) will be passed in
to the respective model hooks, and if a different model is returned,
`setupController` and associated route hooks will re-fire as well.
An example usage of this method is re-querying the server for the
latest information using the same parameters as when the route
was first entered.
Note that this will cause `model` hooks to fire even on routes
that were provided a model object when the route was initially
entered.
@method refresh
@return {Transition} the transition object associated with this
attempted transition
@since 1.4.0
*/
setupController: ->
@store.find('myModel', 1).then (myModel) ->
myModel.reload()
ProductsRoute = Ember.Route.extend
model: ->
@store.find 'product',
activated: true
actions:
accept: (product) ->
if not product.get('activated')
product.set 'activated', true
product.save()
.catch (err) ->
console.log err
product.rollback()
.then =>
@refresh()
ignore: (product) ->
if not product.get('ignored')
product.set 'ignored', true
product.save()
.catch (err) ->
console.log err
product.rollback()
.then =>
@refresh()