Read Model Attributes from controller


#1

Is it possible to read model attributes using eachAttribute(function(name, meta)) in controller or view. I am able to read attributes only in model js file nowhere else.


#2

I’m not sure if I correctly understand your question, but you can access the model and its properties and methods in every part of your controller. For example:

 ...
 var post = this.get('model'); // "this" is the controller here
 post.eachAttribute(function(attribute) {
      console.log(name, meta);
 });

#3

Thanks for the response. Let me explain what I am trying to achieve

My model looks like following

Model:


import DS from ‘ember-data’;

var School = DS.Model.extend({ code: DS.attr(‘string’), name: DS.attr(‘string’), createdAt: DS.attr(‘date’), updatedAt: DS.attr(‘date’)
});

export default School;


Controller: I have defined ‘model’ in the route using this.store.find method.


import Ember from ‘ember’;

export default Ember.ArrayController.extend({ sortProperties: [‘code’], sortAscending: true, sourceCount: function(){ return this.get(‘model.length’); }.property(’@each’) });


Now in the school template. I want to access the model attribute names so that I can dynamically create table heading with attribute names of the model instead of typing them. This is where I am having trouble accessing attributes property of a model.

Thanks Omkar


#4

Just do it this way in your template for example

 <div>
     School: {{model.name}} (code: {{model.code}})
 </div>
 ...