2015-06-26 3 views
0

Я поставил две модели с Имеет много отношениеДоступ к иной модели от маршрутизатора в Ember кли

// 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') 

}); 

Я веду записи через загруженную 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 маршрут следующим образом-

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 модели, но как я могу получить доступ поля от любимой модели?

Благодаря

ответ

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}}