mardi 8 novembre 2016

Ember Model with Nested hasMany or belongsTo relationship

If you want to setup a model with a belongsTo or hasMany relationship, you can do the following:

export default Model.extend({
    "client": DS.belongsTo('client',{
        async: true
    }),
    "days": attr(''),
    "cars": DS.hasMany('car',{ // somehow nest within days
        async: true
    }) 
});

However, what If I have a dynamic form? For example, I have a parking lot full of spaces. I create a new record to assign a client to my parking lot. I then start to assign spaces on different days of the week. Within each space, I want to know what car is parked where on a given day, allowed at a particular time.

Afaik, the JSON output would be something like this:

{
  "client" : "-EADn0NoAq65GRGOU-oQ",
  "days" : [ {
    "date" : "2016-11-11",
    "times" : [ {
      "when" : "morning",
      // want to somehow define a hasmany for which cars are being parked and in which lot number here
    } ]
  }, {
    "date" : "2016-11-12",
    "times" : [ {
      "when" : "afternoon"
      // want to somehow define a hasmany for which cars are being parked and in which lot number here
    } ]
  } ]
}

it looks like within a model, you only define the 'top level'. How do you define a relationship that goes beyond that top level? I want to be able to access a particular day, and then time... to see what cars are parked where, and with the hasMany relationship, I can then pull info from the car within another model... car type, model, milage, color etc.




Aucun commentaire:

Enregistrer un commentaire