簡體   English   中英

控制器中的一系列功能的Ember訪問控制器

[英]Ember access controller from an array of functions in this controller

我正在努力訪問對象:在控制器中,我有一個函數數組,function1接收一個對象,如何從當前模板的該控制器中訪問該對象?

function1中“ this”的范圍是functionsArray,因此,它僅具有function1和function2方法,因此無法在function1中設置像這樣的控制器變量:this.set('data',data_to_pass); -它說“ this.set不是函數”。

我從另一個組件接收到data_to_pass,因此一個解決方案是將該控制器傳遞給該組件,然后將其傳遞回function1中,但是從性能的角度來看,這將是一個錯誤的決定。

 export default Ember.Controller.extend({ t: [], // not visible inside functionsArray functionsArray: { function1(data_to_pass) { // how would I pass data_to_pass to this controller or template here? this.set('data', data_to_pass); // doesn't work, scope of "this" is functionsArray console.log('data' + data_to_pass.fullName); }, function2() { }, } }); 

嘗試這個:

t: [], // not visible inside functionsArray

functionsArray: Ember.Object.create({

  function1: function(data_to_pass) {

  // how would I pass data_to_pass to this controller or template here?
  this.set('data', data_to_pass); // doesn't work, scope of "this" is functionsArray
  alert(this.get('data'));
  },

  function2: function() {

  },

}),

actions: {
   press: function() {
       this.functionsArray.function1('works!');
   }
},


<script type="text/x-handlebars" data-template-name="index">
   <button {{action "press"}}>Press here</button>
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM