0

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?

kamrza
  • 25
  • 1
  • 9

1 Answers1

0

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.

Ember Freak
  • 12,918
  • 4
  • 24
  • 54
  • It looks you already posted these kind of similar question, i would encourage you to use mebr-twiddle.com and share it to us. that will help to sort out problem you are facing – Ember Freak May 23 '17 at 07:18
  • here you go, hope there is everything that's needed: https://ember-twiddle.com/be6fdaa3d09e60a1e1dcfd352e7fd103?openFiles=controllers.edit.js%2C What exactly I trying to do is access permissions properties of user model. I want to collect these properties and send them to controller. Then, from controller I want to send it to template. In my application I have users with dashboards permissions, now I'm building user edit page and I need to load user dashboard permissions to have possibility of changing them. Also I should have possibility add new dashboards for user. – kamrza May 23 '17 at 10:02
  • Is this a good idea of doing that? I tried it also by using each-in loop. It worked but, I saw user's dashboard permissions, but then I couldn't delete and save them by using buttons. – kamrza May 23 '17 at 11:15
  • I was thinking to do it in the same way as adding a new dashboard. See addDashboard() helper but with variable like dashboard: [], which will have all dashboard of chosen user. – kamrza May 23 '17 at 11:26
  • Thanks a lot for the advice. Can you give me another one? I have user model attribute named: permissions which contains elements in a such way: permissions: identifier1:"role1" identifier2:"role2" How can I access them in my controller, and then push as objects to view? I tried with computed properties but I couldn't make it to work. – kamrza May 25 '17 at 08:39
  • How did you define `permissions ` it in model ? if it is `permissions :DS.attr()`, then you can will get what you sent it from server. suppose server sent `permissions: {identifier1:"role1" identifier2:"role2"}` then can access it just like normal object. I created [Sample twiddle](https://ember-twiddle.com/c949d5e748ac57efe7794bda89c1239a?openFiles=models.dashboard.js%2C) I think which will clear you doubt..if not enhance that twiddle come up with your non working twiddle... – Ember Freak May 25 '17 at 08:46
  • I did it in another way, but without you I would have nothing, so thank you a lot! I'm learning programming for several months and just started my adventure with Ember, so it's a little bit complicated for me. – kamrza May 25 '17 at 11:33
  • Thank you :) Good luck with ember . – Ember Freak May 25 '17 at 11:34