2015-09-23 3 views
0

Мне нужно получить доступ к контексту контроллеров ember из обычного объекта. В настоящее время я сохраняю ссылку на контекст контроллера в методе init(), который кажется немного дерьмовым.Контроллер доступа из вложенного объекта

let self = this //saving the context here 
export default Ember.controller.extend({ 

    init() { 
    this._super(...arguments); 
    self = this; 
    }, 

    settings: { 
    crud: { 
     read: { 
      enabled: true, 
      default() { 
      return self.get('blah.blah'); //Need to access the controller context 
      } 
     } 
    } 
    } 

}); 

Поэтому мне нужен доступ к контроллеру self.get('blah.blah'). Есть лучший способ сделать это?

ответ

0

Используйте замыкание вычисляемого свойства

export default Ember.controller.extend({ 

    settings: Ember.computed(function() { 
    const controller = this; 

    return { 
     crud: { 
     read: { 
      enabled: true, 
      defaults() { 
      return controller.get('blah.blah'); 
      } 
     } 
     } 
    }; 
    }) 

}); 

Доступ к объекту

this.get('settings').crud.read.defaults(); // "blah.blah" 
+0

Ах, конечно, спасибо. – jax

Смежные вопросы