2014-04-28 38 views
1

我正在嘗試重新加載已在服務器上更改過的模型。我的代碼如下:餘燼數據重新加載()undefined

App.CustomersController = Ember.ArrayController.extend({ 
    intervalId: undefined, 
    startRefreshing: function() { 
    var self = this; 
    if (self.get('intervalId')) { 
     return; 
    } 

    self.set('intervalId', setInterval(function() { 
     //self.get('model').update(); 
     self.get('model').reload(); 
    }, 30000)); 
    } 
}); 

App.CustomersRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find('customer'); 
    }, 
    setupController: function(controller, model){ 
    this._super(controller, model); 
    controller.startRefreshing(); 
    }, 

    actions: { 
    reload: function() { 
     this.get('model').reload(); 
    } 
    } 
}); 

你可以看到,我有兩個機制重新載入數據 - 一個定時器,也可以通過在用戶界面中的按鈕觸發的動作。後者正是在這裏的餘燼數據文檔中顯示的:http://emberjs.com/api/data/classes/DS.Model.html#method_reload

兩者都不起作用。在這兩種情況下我都得到了未定義,即返回的模型沒有reload()方法。 update()類型的作品,除非它不刪除已刪除的記錄,並且它不是文檔中建議的內容。在嘗試使用重新加載時,我在這裏做錯了什麼?

我的堆棧:

DEBUG: ------------------------------- 
DEBUG: Ember  : 1.5.1+pre.07fafb84 
DEBUG: Ember Data : 1.0.0-beta.7.f87cba88 
DEBUG: Handlebars : 1.3.0 
DEBUG: jQuery  : 1.11.0 
DEBUG: ------------------------------- 

,我使用的情況下,下面的適配器使任何差異:

App.Store = DS.Store.extend({ 
    // Override the default adapter with the `DS.ActiveModelAdapter` which 
    // is built to work nicely with the ActiveModel::Serializers gem. 
    adapter: '-active-model' 
}); 

回答

1

reload存在上的記錄,不是一個集合。

您需要迭代集合並調用每個記錄上的重新加載。

self.get('model').forEach(function(record){ 
    record.reload(); 
}); 

但我猜你不想浪費回調到服務器。在這種情況下,我建議返回一個過濾器作爲你的模型,然後再次調用服務器的所有記錄。

App.CustomersRoute = Ember.Route.extend({ 
    model: function() { 
    this.store.find('customer'); 
    return this.store.all('customer'); 
    }, 
    setupController: function(controller, model){ 
    this._super(controller, model); 
    controller.startRefreshing(); 
    }, 

    actions: { 
    reload: function() { 
     this.get('model').reload(); 
    } 
    } 
}); 

App.CustomersController = Ember.ArrayController.extend({ 
    intervalId: undefined, 
    startRefreshing: function() { 
    var self = this; 
    if (self.get('intervalId')) { 
     return; 
    } 

    self.set('intervalId', setInterval(function() { 
     self.store.find('customer'); // get all customers again, updating the ones we have 
    }, 30000)); 
    } 
}); 
+0

謝謝。所以self.store.find('customer')確實會調用後端,類似於update()。但是,它不會刪除不在後端模型中的記錄。所以如果我添加一條記錄,我會看到我的記錄數增加1,我在本地看到它。如果我刪除了一條記錄,那麼我的後端有1條記錄,然後我的餘燼模型會記錄下來。我發現這個方法的唯一方法是調用this.store.unloadAll,然後再次加載所有記錄。不幸的是,這會導致令人討厭的閃爍,因爲我所有的記錄都被刪除並重新添加,並且所有視圖都在兩次調用之間更新。 –

+0

我真的希望只有一種方法來重新同步後端模型的燼模型。 –

+0

我一直在使用https://github.com/kurko/ember-sync和indexedDB插件。這仍然是一個在製品,但絕對值得檢查,因爲它正朝着正確的方向前進。 – genkilabs

相關問題