sentinel21
sentinel21

Reputation: 546

REST Adapter model reloading

I'm having some difficulty reloading my ember RESTful models, and I'm not sure why. Here's [conceptually] what I'm trying to do... http://jsbin.com/EfuBiNo/4/edit

The only difference between that code and my code is that I'm not using the FixtureAdapter, I'm using the RESTAdapter. Unfortunately, reloading my RESTful models is causing the number of records in the DS.RecordArray to double. So you can see the console is logging that (on every reload) there are two records in the RecordArray.

When I run this with my RestAdapter, the count goes 2...4...8...16....etc. So I'm not sure why it's doubling them every time, but if anybody has any insight on why -- or better yet, another way to reload these records -- I'd be very grateful. Thanks.

Upvotes: 0

Views: 437

Answers (1)

ahaurw01
ahaurw01

Reputation: 1202

If you need to refresh a collection of records after you've already loaded them, you could do something like:

App.ThingsRoute = Ember.Route.extend({
  model: function () {
    return this.store.find('thing');
  },

  actions: {
    refreshThings: function () {
      var controller = this.controller;
      this.store.find('thing').then(function (things) {
        controller.set('content', things);
      });
    }
  }
});

This will simply fetch all the things again and set the record array as the content on your controller whenever the promise resolves. If the items that come back are already catalogued in your store (the ids are already present) then you won't get a ton of duplicate records hanging around; stuff will just get updated. If there are new records that you didn't previously know about, then you'll get those now on your ThingsController.

This is also useful if you are doing some type of querying:

this.store.find('thing', {color: 'red'})

Upvotes: 4

Related Questions