2015-12-10 38 views
0

我有一个名为'scenario'的单一模型,有一个相当简单的ember-cli应用程序(使用Ember 2.2 & Ember Data 2.2.1)。我有一个路线名称“场景”,列出了所有场景。所有的工作正常。在我的应用程序模板中,我有一个带有按钮的导航栏,触发删除所有场景的操作。我的应用程序控制器代码看起来像这样的行动:无法获得store.findAll的工作

this.store.findAll('scenario').then(function(recs){ 
    recs.forEach (function (rec) { 
     rec.destroyRecord(); 
     }); 
    }, console.log ('findAll Scenario failed')); 

当我运行该代码时,总的console.log打印,表明承诺返回没有履行。但情景模型中有清晰的记录。我可以在屏幕上的“场景”路线中看到它们,也可以在Ember检查器中看到它们。为什么这个'findAll'不能满足?

另一件需要注意的事情:当这段代码运行时,Ember Inspector实际上显示了我试图删除的相同记录。 (例如,如果我有两个分别带有ID号的场景,最初我会在预期的Ember Inspector中看到两行,在这段代码运行后,我看到4行,两个id为23,另外两个id为24)。但在灰烬检查器“模式类型”一栏仍然说的情况(2),这表明只有2条。任何人都知道这是怎么回事?

更新(详细信息)

如果我做了一个卸载如下代替“的findAll”代码片断:

this.store.unload('scenario'); 

我仍然看到在灰烬检查的行,但“模型类型”一栏居然显示“方案(0)”,以及记录不再显示在'场景'路线,这是很好的,当然当我刷新时,记录会回来,因为服务器从来没有通知过编辑删除这些记录。出于某种原因,在'findAll'代码片段之后运行上面的'unload'语句不起作用。

更新(详细信息)

也试过:

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部分。我照彼得建议的那样做,并将我的代码修改为以下内容:

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排留在了Ember的检查员。

我认为在某处Ember Data存在一个错误。但是要把它归结为报告真的很难。

回答

1

您正在直接使用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'); 
    } 
); 
+0

感谢彼得。请参阅我上面的更新。 – ptmoy2

0

允诺的then有两个功能作为它的参数 - 一个函数如果成功,将得到执行,而且如果它失败时执行其他功能。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

正如Petter所示,您需要将您的console.log放入函数中。你也可以做的是在你的控制器上声明两个函数,例如didFindScenariosdidNotFindScenarios,并将它们传递给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)); 
    }, 
} 
+0

请参阅上面的更新。 – ptmoy2

+0

也许在调用'forEach'之前在'recs'上调用'toArray'?看到这篇文章:http://stackoverflow.com/a/25715887/2415849 – kevinyuliawan

+0

尝试了你的评论中提到的toArray方法。不幸的是,它也不起作用。 – ptmoy2