Computed property on model attributes


#1

Good day. I’m writing component, which depend on model’s attributes. (table row, each column - different attribute of a model) I don’t know these attributes before runtime. So i want to use computed property, but how?

columns: [{
    columnHeader: 'Наименование',
    name: 'name'
  }, {
    columnHeader: 'Артикул',
    name: 'model'
  }],

cells: Ember.computed('row.?????????'), function() {
    return this.get('columns').map((column) => {
      return Ember.Object.create({
        data: this.get(`row.${column.name}`),
        isEditing: false
      });
    });
  }), 

row.@each - not work, row.{name,model} - work, but it’s not reusable (in other model, other attributes) i need something like row.${someFunction()}, but it’s not work too.

row is a model record

Help, please


#2

Can row.${someFunction()} be restructured into a computed property?


#3

You could use Ember.defineProperty to create a computed property at runtime within the init of your object.

init() {
  this._super(...arguments);
   
  // unclear what `row` is in your above snippet
  const cpArgs = Ember.A(this.get('columns')).map((col) => `row.${col.name}`);

  cpArgs.push(function() {
    return this.get('columns').map((column) => {
      return Ember.Object.create({
        data: this.get(`row.${column.name}`),
        isEditing: false
      });
    });
  });


  Ember.defineProperty(this, 'cells', Ember.computed.apply(this, cpArgs));
}