2015-12-10 45 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