pouchdb-community / ember-pouch

PouchDB/CouchDB adapter for Ember Data
Apache License 2.0
281 stars 76 forks source link

Best way to load nested models #256

Open srsgores opened 4 years ago

srsgores commented 4 years ago

Confused about the best way to load nested models (parent models which have one or more hasMany relationships). For example:

Project > Timeline > Node > Timeline > etc.

Currently, with ember data's async: true behaviour, any nested model does not load, even if accessed from the template:

{{@timeline.id}}

Where timeline.id renders as expected when the child timeline is first created, but subsequent loads fail in the route via this query:

model(params) {
    return this.store.findRecord("project", params.project_id);
};

Where the model looks like this:

(models/project.js)

import {belongsTo, attr} from "@ember-data/model";
import {Model} from "ember-pouch";

export default class ProjectModel extends Model {
    @attr name;
    @attr description;
    @belongsTo("timeline") timeline;
}

(models/timeline.js)

import {hasMany, belongsTo, attr} from "@ember-data/model";
import {Model} from "ember-pouch";

export default class TimelineModel extends Model {
    @belongsTo("project") project;
}

In the ember docs, they mention JSON-API's include parameter, which isn't available for ember-pouch users as far as I can tell. What method is suggested for loading asynchronous, nested relationships?

jlami commented 4 years ago

I don't understand why a belongsTo would not load with async: true. This should work:

{{project.timeline.name}}

The route not loading the model you request should not be related to async. You also mention hasMany but don't have any in your examples. Could you clarify your problem?