jeudi 5 janvier 2017

Ember Model mismatch

I want to facilitate defining rates by multiple dimensions. in the backend a table is used which has two important columns key and value. value contains json data. this makes it convenient to define rates in multiple ways. one such possibility is shown below

key - user:1
value - 
[
  {
    "id": 1,
    "activity": 1,
    "effectiveDate": null,
    "rate": {
      "currency": "currency:1",
      "amount": 10
    }
  },
  {
    "id": 2,
    "activity": 2,
    "effectiveDate": null,
    "rate": {
      "currency": "currency:1",
      "amount": 20
    }
  }
]

on the UI this needs to be shown across multiple rows. uniqueness is defined by user, activity and effectiveDate. so it will be -

user,  activity,  effectivedate, currency,   amount
user:1 activity:1                currency:1, 10
user:1 activity:2                currency:1, 20

so the model will be as below:

export default DS.Model.extend({
  userId: DS.attr('string'),
  activityId: DS.attr('string'),
  rateAmount: DS.attr('number'),
  effectiveDate: DS.attr('date'),
  rateCurrencyId: DS.attr('string'),
});

how can i transform the single key value from API to the model shown above? a delete of the record in the front end doesn't necessary mean a key value delete.

should i create two models one corresponding to the UI and another for key value and transform updates from one model to another and then save the second model to push it back to server? that seems cumbersome.

i checked transform but that helps if i have to tweak the data type. here number of records of the models don't match.

what is the right way to deal with such scenarios in ember?




Aucun commentaire:

Enregistrer un commentaire