Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sideload ember hasMany and belongsTo Relationship?

I have a Person Model as follows

 App.Person= DS.Model.extend({
      id: DS.attr('string'),   
      name: DS.attr('string'),
      visits: DS.hasMany('App.Visit'),
      events: DS.hasMany('App.Event') ,
      allergies: DS.hasMany('App.Allergies'),
      get_allergies : function(){
    return this.get('allergies').getEach('allergy_name').reduce(function(accum, item) {
      return (accum.length > 0) ? (accum +', '+ item) : (item);
    }, '');
  }.property('[email protected]_name')    
    });


App.Visit = DS.Model.extend({
  visit_id: DS.attr('string'),
  date: DS.attr('date'),     
  admission: DS.belongsTo('App.Admission')
});

App.Admission = DS.Model.extend({
  loc: DS.attr('string'),
  admission_date: DS.attr('date'),
  care_team: DS.belongsTo('App.CareTeam')
});

As you can see Person hasMany "allergies", and along with person, allergies is also getting loaded for me because in the UI I am calling the get_allergies method while other hasMany relationships like "visits" and "events" are not getting loaded.

In UI {{person.get_allergies}}

I tried to sideload the relationships "visits" and "events"(using example on net), but that is not working? Can someone tell what is the proper way of sideloading ember-data because I couldnt find any proper documention with example on net except for few questions on stackoverflow itself?

like image 900
Swapnil Avatar asked Mar 05 '13 07:03

Swapnil


People also ask

What are polymorphic models in Ember?

Polymorphism. Polymorphism is a powerful concept which allows a developer to abstract common functionality into a base class. Consider the following example: a user with multiple payment methods. They could have a linked PayPal account, and a couple credit cards on file.

How do I use Ember data?

It is the main interface you use to interact with Ember Data. In this case, call the findAll function on the store and provide it with the name of your newly created rental model class. import Route from '@ember/routing/route'; export default Route. extend({ model() { return this.


2 Answers

According to the documentation, you should just add additional Visit and Event data in the response from the server.

{
  "person": {
    "id": 1,
    ...
    "event_ids": [5, 6, 7]
  },

  "events": [{
    "id": 5,
    ...
  },
  {
    "id": 6,
    ...
  },
  {
    "id": 7,
    ...
  }]
}

The main point here is that Events data should be outside of Person data.

Do you use the standard REST adapter? Could you please add a sample json returned by the server?

like image 182
Stéphane Blond Avatar answered Nov 05 '22 12:11

Stéphane Blond


To sideload data in my app, I configure ember-data to know about the kinds of things I'll be sideloading. E.g. to sideload events and visits, do this:

DS.RESTAdapter.configure('App.Event', {
    sideloadsAs: 'events'
});

DS.RESTAdapter.configure('App.Visit', {
    sideloadsAs: 'visits'
});

App.Store = DS.Store.extend({
    revision: 11
});

and then in your json can look like this:

{
  "person": {
    "id": 1,
    "event_ids": [5, 6, 7],
    "visit_ids": [1, 2, 3]
  },

  "events": [
    { "id": 5 },
    { "id": 6 },
    { "id": 7 }
  ],

  "visits": [
    { "id": 1 },
    { "id": 2 },
    { "id": 3 }
  ]
}
like image 31
Shreyans Avatar answered Nov 05 '22 12:11

Shreyans