ember.js - Dynamic Properties on Model with EmberJS -
below 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 purpose of example, if had days go 50? rather going line line... day4, day5, day6... there way loop through dynamically? first instinct use mixin, , push these onto object, don't think work if had computed property:
isholiday: function(){ if(this.get('day1') == 'off'){ return true; } }.property('day1'), given 'this' in there , have return, don't believe can 'push' onto model generate 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') });
this strange approach still possible:
var defaults = { firstname: ds.attr('string'), birthday: ds.attr('date'), isholiday: function(){ if(this.get('day1') == 'off'){ return true; } }.property('day1') } var dayprops = {}; var count = 20; while(count--){ dayprops['day' + (count + 1)] = ds.attr('string'); } app.person = ds.model.extend(ember.merge(defaults, dayprops)); instead defining dynamic props, better define day model , have one-to-many relations person:
app.person = ds.model.extend({ days: ds.hasmany('day') }); app.day = ds.model.extend({ person: ds.belongsto('person') });
Comments
Post a Comment