简体   繁体   中英

accessing another models data in ember.js

I have a little asset tracking system that I am trying to build. I have many assets, and I have many tags. Assets have many tags and viceaversa

I would like to be able to select a tag from a list, and display only the assets that belong to the selected tag.

I am having a hard time trying to figure out how to get the select view to show the list of tags. I have a feeling that it has to do with my routes...

I am trying to use this.controllerFor('tags').set('content', this.store.find('tag') to pass the tag data into the assets route, but it doesn't seem to be setting the data properly...

I also realize that I am lacking logic to filter the list.

http://jsfiddle.net/viciousfish/g7xm7/

Javascript Code:

App = Ember.Application.create({
 ready: function() {
    console.log('App ready');
  }
});

App.ApplicationAdapter = DS.FixtureAdapter.extend();

//ROUTER
App.Router.map(function () {
  this.resource('assets', { path: '/' });
  this.resource('tags', { path: '/tags' });
});

//ROUTES
App.AssetsRoute = Ember.Route.extend({
  model: function () {
    return this.store.find('asset');
  },
  setupController: function(controller, model) {
    this._super(controller, model);
    this.controllerFor('tags').set('content', this.store.find('tag') );
  }
});

//Tags Controller to load all tags for listing in select view
App.TagsController = Ember.ArrayController.extend();

App.AssetsController = Ember.ArrayController.extend({
  needs: ['tags'],
  selectedTag: null
});


//MODEL
App.Asset = DS.Model.extend({
    name: DS.attr('string'),
    tags: DS.hasMany('tag')
});

App.Tag = DS.Model.extend({
    name: DS.attr('string'),
    assets: DS.hasMany('asset')
});

//FIXTURE DATA
App.Asset.FIXTURES = [
{
    id: 1,
    name: "fixture1",
    tags: [1,2]
},
{
    id: 2,
    name: "fixture2",
    tags: [1]
},   
{
    id: 3,
    name: "fixture3",
    tags: [2]
}];

App.Tag.FIXTURES = [
{
    id: 1,
    name: 'Tag1',
    assets: [1,2]
},
{
    id: 2,
    name: 'Tag2',
    assets: [1,3]
}];

Moustachioed HTML:

<body>
    <script type="text/x-handlebars" data-template-name="assets">
        {{view Ember.Select
            contentBinding="controller.tags.content"
            optionValuePath="content.id"
            optionLabelPath="content.name"
            valueBinding="selectedTag"
        }}

        <table>
            <tr>
                <td>"ID"</td>
                <td>"Name"</td>
            </tr>
            {{#each}}
            <tr>
                <td>{{id}}</td>
                <td>{{name}}</td>
            </tr>
            {{/each}}
        </table>
    </script>
</body>

In your Ember.Select you have contentBinding="controller.tags.content" you need to use controllers instead of controller . Because needs add the referenced controller in a controllers property. In your case you have needs: ['tags'] in the AssetsController so in the assets template, you just need to use controllers.tags to access that instance.

This is the updated select:

{{view Ember.Select
    contentBinding="controllers.tags.content"
    optionValuePath="content.id"
    optionLabelPath="content.name"
    valueBinding="selectedTag"
    prompt="Select a tag" 
}}

To be able to filter the data, you can create a computed property that depends of selectedTag . And filter the content using the selectedTag value. Like the following:

App.AssetsController = Ember.ArrayController.extend({
  needs: ['tags'],
  selectedTag: null,
  assetsByTag: function() {      
      var selectedTag = this.get('selectedTag');
      var found = [];
      this.get('model').forEach(function(asset) {
          return asset.get('tags').forEach(function(tag) {
              if (tag.get('id') === selectedTag) {
                  found.pushObject(asset);
              }
          });
      });
      return found;
  }.property('selectedTag')
});

And in the template you reference that property in the each helper:

{{#each assetsByTag}}
    <tr>
        <td>{{id}}</td>
        <td>{{name}}</td>
    </tr>
{{/each}}

This is the working fiddle http://jsfiddle.net/marciojunior/gqZj3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM