Zatvobor / ember-couchdb-kit

An Ember.js adapter for Apache CouchDB
MIT License
43 stars 13 forks source link

Getting hasMany relationships to work #105

Closed chriswilks closed 9 years ago

chriswilks commented 10 years ago

Thanks for some great work thus far with this toolkit.

I'm trying to implement a simple hasMany relationship setup. Here are my models:

    var Job = DS.Model.extend({

    "type": DS.attr('string', {defaultValue: 'job'}),
    "job_no": DS.attr('string'),
    "name": DS.attr('string'),
    "customer_id": DS.attr('string'),
    "start_date": DS.attr('date'),
    "site_id": DS.attr('string'),
    "items": DS.hasMany('item', { async: true })

    });
    var Item = DS.Model.extend({

      "type": DS.attr('string', {defaultValue: 'item'}),
      "ident": DS.attr('string'),
      "zone_id": DS.attr('string'),
      "item_description": DS.attr('string'),
      "job": DS.belongsTo('job'),
      "attachments": DS.hasMany("attachment", {async: true})
    });

The route/resource definitions:

  this.resource('jobs', function() {
      this.route('view', {path: '/jobs/view/:job_id'});
      this.route('new');
      this.route('edit', {path: '/jobs/edit/:job_id'});
  });

  // generated by ember-generate --scaffold
  this.resource('items');

The job view route:

var JobsViewRoute = Ember.Route.extend({

    model: function(params) {
        return this.store.find('job', params.job_id);
    }

});

The jobs/view.hbs Template:

<h2>Job</h2>
<p><b>Job no:</b> {{ job_no }}</p>
<p><b>Name:</b> {{ name }}M</p>
<p><b>Customer id:</b> {{ customer_id }}</p>
<p><b>Start date:</b> {{ start_date }}</p>
      {{#if items.length}}
        <ul>
          {{#each item in items}}
            <li>Ident: {{item.ident}} Zone: {{item.zone_id}}</li>
          {{/each}}
        </ul>
      {{/if}}
{{#linkTo "jobs.index"}}←All Jobs{{/linkTo}}
{{#linkTo "jobs.edit" model}}Edit{{/linkTo}}
<a href="#" {{action "destroy"}}>Destroy</a>

I also have some routes for Jobs Index and an Items Index, as well as the associated templates.

Please excuse my confusing terminology... a 'View' in this case is not a View in an Ember context, but a mechanism for viewing a single item or job.

We get a list of all Jobs on the Index page for Jobs, but when we view a specific Job record the fields for related items are blank. However, if we then navigate to the Items Index page, and then back to view a specific job, the related item field values are then populated.

Is there a way to ensure that related hasMany entities are loaded automatically when we first load a record? Is this something we need to do in our CouchDB design document or on the Ember side?

Thanks

Chris

Zatvobor commented 10 years ago

Hey @chriswilks! It could be useful to check https://github.com/roundscope/ember-couchdb-kit/tree/master/example internals. There is a good example of hasMany usage in practice. Let me know if that helps you.

highbeats commented 10 years ago

When async is true the relations are retrieved via separate requests, so the related items fields are populated, well, asynchronously. So make sure your backend returns a field which is an array of related items ids if you set async: true.

Zatvobor commented 9 years ago

moved to #116