mardi 5 janvier 2016

Dynamic Properties on Model with EmberJS

Below is a basic ember model:

App.Person = DS.Model.extend({
  firstName: DS.attr('string'),
  birthday:  DS.attr('date'),
  day1: DS.attr('string'),
  day2: DS.attr('string'),
  day3: DS.attr('string')
});

For the purpose of this example, what if I had days go up to 50? Rather than going line by line... day4, day5, day6... is there a way to loop through dynamically? My first instinct is to use a MIXIN, and push these onto the object, but I don't think it would work if I had computed property:

isHoliday: function(){
    if(this.get('day1') == 'off'){
        return true;
    }
}.property('day1'),

Given that 'this' is in there and we have a return, I don't believe you can simply 'push' this onto the model to generate something like this:

App.Person = DS.Model.extend({
    firstName: DS.attr('string'),
    birthday:  DS.attr('date'),
    day1: DS.attr('string'),
    day2: DS.attr('string'),
    day3: DS.attr('string'),
    isHoliday: function(){
        if(this.get('day1') == 'off'){
            return true;
        }
    }.property('day1')
});




Aucun commentaire:

Enregistrer un commentaire