vendredi 26 juin 2015

Accessing different model from router in ember cli

I have established two models with Has Many relationship

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

 });

and

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

});

I am leading the records via a sideloaded json from my server as follows-

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

The records are getting loaded but I want to know how to access data from model favorite in my userwelcome route. I am setting my userwelcome route as follows-

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


  }

I am able to access fields from userwelcome model but how do I access fields from favorite model?

Thanks




Aucun commentaire:

Enregistrer un commentaire