Search code examples
ember.jsmodelcontrollerember-data

Accessing model properties in Controller - Ember


So, I'm trying to access my model properties in controller.

Controller:

dashobards: [
{ id: 12, name: 'test' },
{ id: 17, name: 'test2' },
];

In route I have model named dashboards

return Ember.RSVP.hash({
dashboards: this.store.findAll('dashboard'),
}).then((hash) => {
  return Ember.RSVP.hash({
    dashboards: hash.dashboards
  });
}, self);

I wanna have result in controller like this:

dashboards: [
{ id: 12, name: 'test' },
{ id: 17, name: 'test2' },
{ id: 17, name: 'test1' },
{ id: 20, name: 'test20' },
];

In controller I am trying to access this model like this:

this.dashborads = this.get(model.dashobards)

And it's not working, is there any other way of doing that?


Solution

  • Another update How to access complex object which we get it from server in ember data model attibute,

    Created twiddle to demonstrate

    define attribute with DS.attr(),

    export default Model.extend({
      permissions:DS.attr()
    });
    

    route file,

    model(){
        return this.store.findAll('dashboard');
      }
    

    Your server response should be like,

    data: [{
            type: 'dashboard',
            id: 1,
            attributes: {
              permissions: {'name':'role1','desc':'description'}          
            }
          }]
    

    hbs file,

    {{#each model as |row| }}
     Name: {{row.permissions.name}} <br/>
     Desc: {{row.permissions.desc}} <br />
    {{/each}}
    

    Update: Still I am not sure about the requirement, Your twiddle should be minimalized working twiddle for better understanding..anyway I will provide my observation,

    1.

    model(params) {
        this.set('id', params.userID);
        const self = this;
        return Ember.RSVP.hash({
            dashboards: this.store.findAll('dashboard'),
            user: this.store.findRecord('user', params.userID)
        }).then((hash) => {
            return Ember.RSVP.hash({
                user: hash.user,
                dashboards: hash.dashboards
            });
        }, self);
    }
    

    The above code can be simply written like

    model(params) {
        this.set('id', params.userID);
        return Ember.RSVP.hash({
            dashboards: this.store.findAll('dashboard'),
            user: this.store.findRecord('user', params.userID)
        });
    }
    
    1. Its good to always initialize array properties inside init method. refer https://guides.emberjs.com/v2.13.0/object-model/classes-and-instances/

    2. For removing entry from array, this.dashboard.pushObject({ 'identifier': '', 'role': '' }); try this this.get('dashboard').pushObject({ 'identifier': '', 'role': '' });.

    if possible instead of plain object you can use Ember.Object like

    this.get('dashboard').pushObject(Ember.Object.create({ 'identifier': '', 'role': '' }));
    
    1. For removing entry.
        removeDashboard(i) {
            let dashboard = Ember.get(this, 'dashboard');
            Ember.set(this, 'dashboard', dashboard.removeObject(dashboard[i]));
        }
    

    The above code can be written like, since i is an index

    removeDashboard(i) {
        this.get('dashboard').removeAt(i)
    }
    

    Just do return this.store.findAll('dashboard'); in route model hook, and dont override setupController hook, then in hbs you should be able to access model that will represent RecordArray. you can have a look at this answer for how to work with this.