Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly create related models #19

Closed
backspace opened this issue Dec 5, 2014 · 1 comment
Closed

How to properly create related models #19

backspace opened this issue Dec 5, 2014 · 1 comment

Comments

@backspace
Copy link
Collaborator

Hello! Thanks for your work on this, I’m happy that Ember Data and PouchDB are closer to working smoothly together.

I’m having trouble understanding how to create related models, which is maybe what #8 was about, but that person’s resolution was unclear to me.

I created a simple example application, which is running here.

Here are the models, representing issues of a magazine and the features within them:

app/models/issue.js:

DS.Model.extend({
  title: DS.attr('string'),
  features: DS.hasMany('feature'),

  rev: DS.attr('string')
});

app/models/feature.js:

DS.Model.extend({
  title: DS.attr('string'),
  issue: DS.belongsTo('issue'),

  rev: DS.attr('string')
});

Nothing special.

My question is: is it expected behaviour that an issue is always stored in the database with features as an empty array? When you visit the example application, you can see that it thinks, upon load, that the lone issue has 0 features. When you click the Load features button, it runs store.find('feature'), and suddenly the feature is connected to its parent issue.

There’s a button to print the contents of the PouchDB, here’s example output:

{
    "total_rows": 2,
    "offset": 0,
    "rows": [
        {
            "id": "feature_2_4CDC71E9-08EC-2E42-B167-F4B3B495DBA4",
            "key": "feature_2_4CDC71E9-08EC-2E42-B167-F4B3B495DBA4",
            "value": {
                "rev": "1-c9bd4bfcd6c416a54c5783ceaac787b1"
            },
            "doc": {
                "data": {
                    "title": "A feature",
                    "issue": "A6A770A8-E416-CB0B-900A-58CF2809A369"
                },
                "_id": "feature_2_4CDC71E9-08EC-2E42-B167-F4B3B495DBA4",
                "_rev": "1-c9bd4bfcd6c416a54c5783ceaac787b1"
            }
        },
        {
            "id": "issue_2_A6A770A8-E416-CB0B-900A-58CF2809A369",
            "key": "issue_2_A6A770A8-E416-CB0B-900A-58CF2809A369",
            "value": {
                "rev": "2-c05d1fb71749477e6c695283b7bac761"
            },
            "doc": {
                "data": {
                    "title": "An issue",
                    "features": []
                },
                "_id": "issue_2_A6A770A8-E416-CB0B-900A-58CF2809A369",
                "_rev": "2-c05d1fb71749477e6c695283b7bac761"
            }
        }
    ]
}

Maybe I am constructing the models in the wrong way? In my real application, I’m constructing test models in acceptance tests. For the example application, I made an initialiser to do the same:

{
  name: 'data-populator',
  after: 'store',

  initialize: function(container, application) {
    application.deferReadiness();

    var store = container.lookup('store:main');

    var issues = store.find('issue');
    issues.then(function() {
      if (issues.get('length') == 0) {
        var issue = store.createRecord('issue', {title: 'An issue'});

        issue.save().then(function() {
          var feature = store.createRecord('feature', {title: 'A feature'});
          issue.get('features').pushObject(feature);
          feature.save();
          issue.save().then(function() {
            application.advanceReadiness();
          });
        });
      }
      else {
        application.advanceReadiness();
      }
    });
  }
}

I’ve tried a variety of ways of constructing the models, like passing in the issue when creating a feature:

var feature = store.createRecord('feature', {issue: issue, title: 'A feature'});

and many other variations, but never have I seen the features array be anything but empty in the database.

I’ve been using weird workarounds like pre-loading all features in the issues route, and similarly for other related models, but I suspect I’m just missing something.

@rsutphin
Copy link
Collaborator

rsutphin commented Dec 5, 2014

I think you're running into #16. I just added a comment there with the workaround I'm using at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants