lundi 27 mars 2017

Dynamically define properties to Ember Data model

I'm working on a data model that has a set of predefined attributes that will always be present, and other custom properties defined for it by the user.

A company has many roles. Each company using this system will want to define custom properties for all its roles, in addition to the fixed set of properties that all roles have across different companies.

The idea would be that then roles payload in json-api come with all the attributes, custom or not:

{
  "id": "123",
  "type": "roles",
  "attributes": {
    "name": "CEO",
    "salary": 100000,
    "favoriteColor": "blue"
  }
}

In the above role, name and salary are default attributes, present in all roles regardless company, but favoriteColor is a custom attribute that the particular company owning this role defined as something that they needed to have for all its roles.

I'm wondering if I could get away with something like this using Ember Data, given that I won't be able to define these custom attributes in the role model definition:

// app/models/role.js
export default DS.Model.extend({
  name: DS.attr('string'),
  salary: DS.attr('number'),
})

To make matters even worse, the idea is that these custom attributes will not necessarily be string values, but they can have their type specified too. So a company may want to have favoriteColor of type string, and also birthDate of type date.




Aucun commentaire:

Enregistrer un commentaire