[英]Ember access controller attribute within common route/controller mxiin
我在控制器中將屬性定義為
myControllerAttr
路由/控制器( my-mixin.js
)擴展了一個通用的mixin
現在在my-mixin.js中,有兩種方法可以從route / controller類中調用
我的問題是在這些mixin方法中,如何訪問控制器屬性myControllerAttr
由於this.myControllerAttr
可能無法始終正常工作
這取決於該方法是從路由類還是從控制器類調用的
我應該添加一個if條件,還是最好的方法是什么?
綜上所述,我的問題是如何檢查
this.get('myControllerAttr') V/s
this.controllerFor(this.routeName).get('myControllerAttr')
不知道這是否正是您所需要的,但是可能是。
// mixins/type-checker.js
export default Ember.Mixin.create({
isRoute: computed('target',function(){
const isUndefined = typeof this.get('target') === 'undefined'
return isUndefined ? true : false
}),
isController: computed('target',function(){
const isUndefined = typeof this.get('target') === 'undefined'
return isUndefined ? false : true
}),
getAttribute(attr){
let attrYouWant
if(this.get('isController')){
attrYouWant = this.get(attr)
}else{
attrYouWant = this.controllerFor(this.routeName).get(attr)
}
return attrYouWant
}
})
然后您可以像這樣使用它:
//routes/application.js
import TypeChecker from '../mixins/type-checker'
export default Ember.Route.extend(TypeChecker, {
actions: {
test(){
const testProp = this.getAttribute('prop')
console.log(testProp)
}
}
})
這里是一個玩弄 ,我已經實現了我的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.