我有一个相当简单的成员-cli应用程序(使用Ember 2.2和Ember数据2.2.1),只有一个模型名为“场景”。我有一个路线名称‘方案’,列出了所有的方案。一切都很好。在我的应用程序模板中,我有一个带有按钮的导航栏,该按钮可以触发删除所有场景的操作。我的应用程序控制器代码如下所示:
this.store.findAll('scenario').then(function(recs){
recs.forEach (function (rec) {
rec.destroyRecord();
});
}, console.log ('findAll Scenario failed'));
当我运行这段代码时,console.log总是打印,表示返回的承诺没有实现。但是在场景模型中显然有记录。我可以看到他们在屏幕上的“场景”路线,也在安博检查员。为什么这个“findAll”没有实现呢?
另外要注意的是:当运行这段代码时,Ember检查器实际上显示了我试图删除的相同的记录。(例如,如果我有两个I分别为23和24的场景,那么最初我会在Ember检查器中看到预期的两行。在运行这段代码之后,我会看到4行,其中2行为id 23,2行为id 24)。但是Ember检查器中的“模型类型”列仍然显示了场景(2),只显示了2条记录。有人知道发生了什么事吗?
更新(更多信息)
如果我按以下方式卸载,以代替“findAll”代码片段:
this.store.unload('scenario');
我仍然可以在Ember检查器中看到行,但是“模型类型”列实际上显示了‘场景(0)’,并且记录不再显示在‘场景’路由中,这是很好的。当然,当我进行刷新时,记录会返回,因为服务器从未收到删除这些记录的通知。由于某些原因,在“findAll”代码段之后运行上述“卸载”语句没有任何效果。
更新(更多信息)
也曾尝试过:
this.store.findAll('scenario').then(function(recs){
recs.forEach (function (rec) {
Ember.run.once (this, function() {
rec.destroyRecord();
});
});
}, console.log ('findAll Scenario failed'));
同样的错误。
更多更新
我对console.log电话的疏忽。我按照Peter的建议做了,并将我的代码修改如下:
this.store.findAll('scenario').then(function(recs){
recs.forEach (function (rec) {
Ember.run.once (this, function() {
rec.destroyRecord();
console.log ('found');
});
});
}, function() {
console.log ('findAll Scenario failed');
}
);
这段代码验证了findAll确实找到了记录。但最初的问题只存在于略有不同的交响乐中。我添加了两个记录,然后运行上面的代码。Ember没有删除这两条记录,而是像以前一样添加了两行;“模型类型”仍然显示了场景(2)。但是,如果我再次运行相同的代码,“模型类型”将转到场景(0),并且记录不再显示在场景路由中。但这4排仍留在安伯巡查员手中。
我想在某个地方的安博数据里有个漏洞。但是很难把它确定下来报告。
发布于 2015-12-10 15:00:48
您直接使用的是console.log
语句,其中应该有一个回调函数。因此,findAll Scenario failed
将始终执行。
试试这个:
this.store.findAll('scenario').then(
function(recs){
recs.forEach (function (rec) {
Ember.run.once (this, function() {
rec.destroyRecord();
});
});
}, function () { // <-- callback
console.log ('findAll Scenario failed');
}
);
发布于 2015-12-11 04:50:15
允诺的then
使用两个函数作为其参数--一个函数如果成功将被执行,另一个函数在失败时执行。Objects/Promise/then
正如Petter所展示的,您需要将您的console.log
放在一个函数中。您还可以在控制器上声明两个函数,例如didFindScenarios
和didNotFindScenarios
,并将它们传递到then
函数中,尽管如果要确保它们对应用程序控制器具有正确的this
引用,那么对它们调用.bind(this)
是很重要的。
例如,在应用程序控制器中:
didFindScenarios: function(recs){
recs.forEach (function (rec) {
Ember.run.once (this, function() {
rec.destroyRecord();
});
});
this.set('isProcessing', false); //we have the 'this' that refers to this application controller since we called `.bind(this)` on this function
},
didNotFindScenarios: function(err){
console.log ('findAll Scenario failed. Here is the error: ' + err);
this.set('isProcessing', false);
},
actions: {
deleteAllScenarios: function(){
this.set('isProcessing', true); //just an example using 'this'
this.store.findAll('scenario').then(this.didFindScenarios.bind(this), this.didNotFindScenarios.bind(this));
},
}
https://stackoverflow.com/questions/34191597
复制相似问题