ember.jsember-dataember-cliember-modelember-controllers

Ember.js - Controller not picking model data


I have a model which has this field

diaryItemLabel: DS.attr('string'),

I want to access the data from this field in my controller.

Controller.js

enter image description here

I want to replace the 'Add' with whatever data I get from diaryItemLabel.

I cannot use this.get('model.diaryItemLabel'). It gives me this.get() is not a function. I tried using Ember.get('model', 'diaryItemLabel'). It gives me empty string.

Edit 1: Set default to 'Add' if model.diaryItemLabel variable is empty.

Could someone guide me to the right direction?

Thanks


Solution

  • Controller will get model property only after setupController hook which will be called after model hook.

    Create computed property which dependant on model.diaryItemLabel in controller which will return your required object

    confirm:Ember.computed('model.diaryItemLabel',function(){
     let temp = {};
     temp.accept= { text: Ember.isEmpty(this.get('model.diaryItemLabel')) ? 'Add' : this.get('model.diaryItemLabel'), buttonClass: 'btn btn-primary'};
     temp.reject = { text: 'cancel', buttonClass: 'btn btn-default'};
     return temp;
    }