0
votes

My app has an index page listing all items, when clicking an item I want to reload the model for that item, as the single-model API endpoint sideloads the relations.

But I only want to reload the model if they aren't already fetched.

Is there any way to achieve this? I've tried something like:

return this.store.find('todo-list', params.id).then((model) => model.reload());

but that didn't seem to work + it will always reload regardless of the relations already being loaded.

Ember.JS and Ember-Data Version: 1.13.5

1
What versions of Ember and Ember Data are you using? - locks
@locks Both are 1.13.5 - 3onyc

1 Answers

0
votes

The following works as intended:

import Ember from 'ember';

export default Ember.Route.extend({
    setupController(controller, model) {
        if (!model) {
            return controller.set('model', this.store.find('todo-list', model.id));
        }

        if (!model._internalModel ||
            !model._internalModel._relationships ||
            !model._internalModel._relationships.initializedRelationships
        ) {
            return controller.set('model', this.store.fetchById('todo-list', model.id));
        }

        var relationships = model._internalModel._relationships.initializedRelationships;
        if (relationships.groups.canonicalState[0].dataHasInitialized) {
            return;
        }

        return controller.set('model', this.store.fetchById('todo-list', model.id));
    }
});

However it uses internal structures, which isn't preferred, any better way of doing this?