在ember cli中从路由器访问不同的模型

时间:2015-06-26 21:37:01

标签: ember.js ember-data ember-cli

我已经建立了两个具有很多关系的模型

// app/models/userwelcome.js
export default DS.Model.extend({
  favorites:DS.hasMany('favorite'),
  brandcredits:DS.attr(),
  token: DS.attr('string'),
  fullname:DS.attr('string'),
  userimglocation:DS.attr('string'),
  city:DS.attr('string'),

 });

// app/models/favorite.js
export default DS.Model.extend({

  Brandname: DS.attr('string'),
  Imglocation: DS.attr('string'),
  Checked:DS.attr('string'),
  Createdate:DS.attr('date'),
  userwelcome: DS.belongsTo('userwelcome')

});

我通过我的服务器的sideloaded json引导记录,如下所示 -

{ userwelcome: 
   [ { _id: 558ce2af106319b412e48b67,
       userimglocation: '/images/user_imgs/ppl5.jpg',
       city: 'Mountain View',
       fullname: 'Sansa Stark',
       favorites: 
        [ '5586da238a60ebcb7abeb733',
          '5586da128a60ebcb7abeb732',
          '558b382e7881f424154d6c27',
          '558b38467881f424154d6c28',
          '558b38687881f424154d6c29' ],
       brandcredits: 
        [ { brand_id: '5586da128a60ebcb7abeb732',
            brand_credits: 123,
            _id: 558ce2af106319b412e48b6c },
          { brand_id: '5586da238a60ebcb7abeb733',
            brand_credits: 500,
            _id: 558ce2af106319b412e48b6b },
          { brand_id: '558b382e7881f424154d6c27',
            brand_credits: 500,
            _id: 558ce2af106319b412e48b6a },
          { brand_id: '558b38467881f424154d6c28',
            brand_credits: 500,
            _id: 558ce2af106319b412e48b69 },
          { brand_id: '558b38687881f424154d6c29',
            brand_credits: 245,
            _id: 558ce2af106319b412e48b68 } ] } ],
  favorite: 
   [ { _id: 5586da128a60ebcb7abeb732,
       Checked: false,
       Imglocation: '/images/banana.png',
       Createdate: 'Sun Jun 21 2015 08:36:50 GMT-0700 (PDT)',
       Brandname: 'Banana Republic',
       __v: 0 },
     { _id: 5586da238a60ebcb7abeb733,
       Checked: false,
       Imglocation: '/images/gap1433683451312.png',
       Createdate: 'Sun Jun 21 2015 08:37:07 GMT-0700 (PDT)',
       Brandname: 'GAP',
       __v: 0 },
     { _id: 558b382e7881f424154d6c27,
       Checked: false,
       Imglocation: '/images/hugoboss1433683671484.png',
       Createdate: 'Wed Jun 24 2015 16:07:26 GMT-0700 (PDT)',
       Brandname: 'Hugo Boss',
       __v: 0 },
     { _id: 558b38467881f424154d6c28,
       Checked: false,
       Imglocation: '/images/levis1433683501618.png',
       Createdate: 'Wed Jun 24 2015 16:07:50 GMT-0700 (PDT)',
       Brandname: 'Levis',
       __v: 0 },
     { _id: 558b38687881f424154d6c29,
       Checked: false,
       Imglocation: '/images/lululemon.jpg',
       Createdate: 'Wed Jun 24 2015 16:08:24 GMT-0700 (PDT)',
       Brandname: 'Lulu Lemon',
       __v: 0 } ] }

记录已加载但我想知道如何从我的userwelcome路线中的模型收藏夹访问数据。 我正在设置我的userwelcome路线如下 -

  model: function() {
   var token=this.controllerFor('index').get('token');

    console.log(token);

    var self=this;

    var inflector = Ember.Inflector.inflector;
    inflector.irregular('userwelcome', 'userwelcome');
      return this.store.find('userwelcome',{'token':token});


  }

我可以从userwelcome模型访问字段,但如何从喜欢的模型中访问字段?

由于

1 个答案:

答案 0 :(得分:0)

有很多选项,例如,您可以使用store.all

// route
var self = this;
model: function() {
   // ...
   return this.store.find('userwelcome',{'token':token}).then(function(records) {
     return Ember.RSVP.hash({
       model: records,
       favorites: self.store.all('favorite');
     });
   });
},

setupController: function(controller, models) {
  controller.setProperties(models);
}

// template
{{#each favorites as |f|}}
  {{!-- ... --}}
{{/each}}