2

With the following queries exposed by my back end:

  • GET /api/foos
    • returns a list of all Foos, suitable for display within a master list
  • GET /api/foos/:foo_id
    • returns a single Foo, with more detailed information, suitable for display within a detail view

My front end displays a list of Foos on the left and when one of them is clicked, the outlet on the right (in the outlet) displays its detailed version.

{{#each foo in model}}
    {{#link-to 'foo' foo}}{{foo.name}}{{/link-to}}
{{/each}}
{{outlet}}

I am using ember-model to store my models, and have implemented App.Foo.adapter's find and findAll hooks, such that when they are invoked, they hit the back end APIs described above correctly.

When my app hits the GET /api/foos (via findAll) first, and then subsequently when the user clicks on the Foo, and ember-model doesn't hit GET /api/foos/:foo_id because it doesn't invoke the find hook, because it realises that that particular model is already in the cache.

This is great, because why fetch something again, when we know that we already have it in memory.

However, in my app, this assumption is insufficient. I have to further check if I have got the full version of that Foo, e.g. !!aFoo.get('propertyOnlyInDetailedVersion'), and otherwise I would like to force Foo to fetch again.

How can I go about doing this - how do I make ember-model re-fetch an object that has already been fetched prior?

bguiz
  • 27,371
  • 47
  • 154
  • 243

2 Answers2

1

This used to be a known issue, but this got fixed recently.

Taken in verbatim from: https://github.com/ebryn/ember-model/pull/297 (ckung's comment)

Developers can define "transient: false" on their classes that extend Ember.Model, this will skip the cache check.

The commit: https://github.com/ckung/ember-model/commit/dc46171d2121630e62a130d58dd6b709c6c00541

Update your ember-model to the relevant version and you can get this working.

--

Edit: Sorry, I thought the next block text made my last edit -- but alas it didn't.

So my idea now is to do something like CachedModel extends LiveModel extends Ember-Model. Then set transient to true and false respectively.

Seth Malaki
  • 4,436
  • 23
  • 48
  • Thanks for the answer. However, I actually do want to keep this cached version, and instead perform a test of my own in order to determine whether it should be re-fetched, as described above. Essentially I would like to be able to decide whether it is 'transient' each time `find` is called. Have you got any suggestion on how I can go about doing this? – bguiz Mar 07 '14 at 04:18
  • @ seth - Thanks for pointing me in the right direction. I have [answered here](http://stackoverflow.com/a/22241774/194982) with my own solution. I figure that mine is too specific of a use case to submit a PR to `ember-model` over - so I'm just going to go with this as a "works-for-me" thing. +1 and check to you, for pointing me in the right direction. – bguiz Mar 07 '14 at 04:57
  • I thought my answer was a bit more complete.. So I edited my answer to what my initial thoughts were. But I see you already have a solution! – Seth Malaki Mar 07 '14 at 06:01
  • @ seth - yeah I see where you are going with that.. Ping me here if you submit a PR to ember-model with a generic version of that or something, I'd be keen to know. – bguiz Mar 08 '14 at 01:28
1

So this was what I ended up going with in the end:

App.FooDetailRoute = Ember.Route.extend({
    model: function() {
        var foo = this.modelFor('foo');
        if (!foo.get('propertyOnlyInDetailedVersion')) {
            //if foo does not have propertyOnlyInDetailedVersion property, we know that only the
            //summarised foo has been last fetched.
            //to force a re-fetch, we trick ember-model by setting isLoaded to false
            //before triggering fetch once more
            foo.set('isLoaded', false);
            var refetchedFoo = App.Foo.find(parseInt(foo.get('id'), 10));
            return refetchedFoo ;
        }
        return foo;
    }
});

tl;dr = set model.isLoaded to false and call model.find again.

bguiz
  • 27,371
  • 47
  • 154
  • 243