1
votes

I am trying to reload a model that has changed on the server. My code is as follows:

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();
    }
  }
});

You can see that I have two mechanisms for reloading the data - one a timer, and also an action triggered by a button in the UI. The latter is exactly what is shown in the ember-data documentation here: http://emberjs.com/api/data/classes/DS.Model.html#method_reload

Neither works. I get undefined in both cases i.e. the model returned does not have a reload() method. update() sort of works, except it does not remove deleted records and it is not what is recommended in the documentation. What am I doing wrong here in trying to use reload?

My stack:

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: ------------------------------- 

and I am using the following adapter in case that makes any difference:

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

1 Answers

1
votes

reload exists on a record, not a collection.

You would need to iterate the collection and call reload on each record.

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

But I'm guessing you don't want to waste the callbacks to the server. In this case I'd recommend returning a filter as your model, then make another call to the server for all records.

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 ) );
  }
});