Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top