# Another hack to refill partially-loaded Models

**URL:** <https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375>\
**Category:** Ember Data\
**Created:** [May 30, 2013, 4:46pm UTC](https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375 "2013-05-30T16:46:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![suckerpunch](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@suckerpunch](https://discuss.emberjs.com/u/suckerpunch)\
**Post date:** [May 30, 2013, 4:46pm UTC](https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375/1 "2013-05-30T16:46:34Z")

</div>

Since Ember doesn’t yet auto-reload models to fill in missing data, here’s my simple workaround.

Basically, I look to see if one of the missing fields is null, and if so, `reload()`

```
App.StoriesRoute = Ember.Route.extend({
  model: function() {
    return App.Story.find();
  }
});

App.StoryRoute = Ember.Route.extend({
  model: function(params) {
    var record = App.Story.find(params.story_id);
    var r = record;
    if(r.get('subtitle') == null) {
      r.reload();
    }
    return record;
  }
});

```

It’s less of a hack than having two models or changing things on the server. Ideally we could mark said model property as read-only too, e.g.

```
App.Story = DS.Model.extend({
  title: DS.attr('string'),
  subtitle: DS.attr('string').readOnly()
});

```

Anyone have any lower-level ideas?

---

<div class="post-metadata">

**Author:** ![lukegalea](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lukegalea/32/4306_2.png) [@lukegalea](https://discuss.emberjs.com/u/lukegalea)\
**Post date:** [June 1, 2013, 12:54am UTC](https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375/2 "2013-06-01T00:54:14Z")

</div>

Funny, I implemented this earlier this week. I guess it’s a common problem.

The approach I took was to add a “fictitious” property on the server side via ActiveModel::Serializers. I called it isHeader to indicate that it’s an incomplete model (ie: just a header).

I have two serializer classes, one that returns a full model with isHeader false, and one that returns a subset of the data with isHeader true.

In setupController, I check if isHeader is false… and if it is I call reload.

I was originally basing it off a particular value like you are, but I was concerned that I couldn’t tell the difference between the field being set to null or empty, vs. not being provided due to a partial load.

---

<div class="post-metadata">

**Author:** ![gunn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/gunn/32/14511_2.png) [@gunn](https://discuss.emberjs.com/u/gunn)\
**Post date:** [June 28, 2013, 7:27am UTC](https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375/3 "2013-06-28T07:27:06Z")

</div>

Here’s the approach I’m using. it’s similar to @suckerpunch’s, but the reload happens on ‘setupController’ and I’m careful to make sure that the record loading doesn’t delay rendering. Also, ‘reload’ didn’t work with a custom slug, so I do a manual find.

```auto
// Load the light version of all subjects on page load
App.ApplicationController = Em.Controller.extend({
  init: function() {
    return App.Subject.find();
  }
});

// Fetch all our previously loaded subjects
App.SubjectsRoute = Ember.Route.extend({
  model: function() {
    return App.Subject.all();
  }
});

App.SubjectRoute = Ember.Route.extend({
  // If we're loading the page directly to this route, do a normal find
  model: function(params) {
    return App.Subject.find(params.subject_id);
  },
  setupController: function(controller, model) {
    // Show what details we have for this subject (e.g. the title) immediately
    controller.set("model", model);

    // Load full details for the model and display them as soon as they arrive
    if (Em.isEmpty(model.get("text"))) {    
      App.Subject.find(model.get("id")).then(function(model) {
        return controller.set("model", model);
      });
    }
  }
});

```

---

<div class="post-metadata">

**Author:** ![watsonbox](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/watsonbox/32/9156_2.png) [@watsonbox](https://discuss.emberjs.com/u/watsonbox)\
**Post date:** [June 16, 2014, 9:25am UTC](https://discuss.emberjs.com/t/another-hack-to-refill-partially-loaded-models/1375/4 "2014-06-16T09:25:30Z")

</div>

I solved the same problem recently in a similar way, by using a ‘partial’ attribute to indicate that model data is not complete. I then call `reload()` in the detail view if the model is partial, but also make sure to not overwrite complete models with partial data should the list data be fetched afterwards.

See my [blog post](http://watsonbox.github.io/blog/2014/06/13/lazy-and-partial-data-loading-with-ember-dot-js-and-rails/) for a full write-up.
