在海市蜃楼中更改数据后的余烬刷新/重装模型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在海市蜃楼中更改数据后的余烬刷新/重装模型相关的知识,希望对你有一定的参考价值。
在我做了更改之后,我需要再次刷新/重新加载我的模型钩子,例如通过动作添加或编辑我的模态。我已经看过很多类似这样的主题,但是在我的案例中有不赞成或不适用。
这是我的component.js文件,名为“listing.js”:
import Component from '@ember/component';
export default Component.extend({
actions: {
togglePower: function(a){
console.log(a);
var data = new Array();
data.push(a);
$.ajax({
cache:false,
type: 'POST',
url: "/api/device",
data: JSON.stringify(data),
contentType: "application/json"
})
this.get('users.single.households.single.rooms.single.devices.single').send('refresh');
}
}
});
Mirage收到我的数据并能够使用它。但是现在我需要重新加载我的页面以查看我的更改。所以我试着在listing.js中添加最后一行
this.get('users.single.households.single.rooms.single.devices.single').send('refresh');
但这对我不起作用。控制台提供:
TypeError:this.get(...)未定义
这是我的路线:
model(){
...},
actions: {
refresh: function() {
this.refresh();
}
}
> }
答案
要调用refresh,您必须使用闭包操作。在组件文件中的ajax之后,您需要这样做
this.get('on-refresh')();
'on-refresh'是传递给组件的函数。在这种情况下,它是刷新操作。
{{listing on-refresh=(route-action "refresh")}}
route-action
来自这个附加的https://github.com/DockYard/ember-route-action-helper,它允许你直接从模板调用路由动作而不通过控制器。
所以我们在这里做的是将刷新函数传递给组件,然后组件在需要时调用它。
以上是关于在海市蜃楼中更改数据后的余烬刷新/重装模型的主要内容,如果未能解决你的问题,请参考以下文章
当另一个片段中的数据发生更改时,如何刷新一个片段中的 RecyclerView
当sqlite android片段中的数据更改或删除时如何刷新recyclerview?