2016-07-24 2 views
0

나는 엠버 (ember)를 처음 접했고, 나의 액션 중 하나에서 별도의 액션 (셔플로 표시)을 사용하고 싶습니다. 내가 여기서 뭔가 잘못하고 있다는 것을 알고, 셔플 방법은 실제로 아무것도하지 않습니다. 나는 그것을 잘못 부르고 있습니까, 아니면 다른 행동에 의해 사용되어서는 안되는 행동입니까? 여기에 코드입니다 :컨트롤러 내부에서 작업을 사용 하시겠습니까?

import Ember from 'ember'; 

export default Ember.Controller.extend({ 

    taskData: [], 
    personData: [], 
    taskIn: ' ', 
    personIn: ' ', 



    actions: { 

    saveTask() { 

     const task = this.get("taskIn"); 
     this.taskData.push(task); 
    }, 

    savePerson() 
    { 
     const person = this.get("personIn"); 
     this.personData.push(person); 
    }, 

    print(){ 
     var taskString; 

     //this.taskData.remove(0); 
     for(var i = 0; i < this.taskData.length; i++) 
     { 
      taskString = taskString + this.taskData[i]; 
     } 
     alert(taskString); 
     //alert(this.personData); 
    }, 

    shuffle(array) { 
     var currentIndex = array.length, temporaryValue, randomIndex; 

     // While there remain elements to shuffle... 
     while (0 !== currentIndex) { 

      // Pick a remaining element... 
      randomIndex = Math.floor(Math.random() * currentIndex); 
      currentIndex -= 1; 

      // And swap it with the current element. 
      temporaryValue = array[currentIndex]; 
      array[currentIndex] = array[randomIndex]; 
      array[randomIndex] = temporaryValue; 
     } 

     return array; 
    }, 

    //algorithm to match up tasks with people 
    assign(){ 


     var newTaskD = this.shuffle(this.taskData); 
     var newPersonD = this.shuffle(this.personData); 
     var taskString = ''; 
     var peopleString = ''; 

     for(var i = 0; i<newTaskD.length; i++) 
     { 
      taskString += " " + newTaskD[i]; 

     } 

     for(var j = 0; j<peopleString.length; j++) 
     { 
      peopleString += " " + newPersonD[j]; 

     } 

     alert(peopleString); 
     alert(taskString); 

    } 
    } 
}); 
+0

인터페이스가 '셔플'동작을 트리거해야합니까? 아니면 적어도 하나의 액션을 호출해야하는 메소드를 설정하려고합니까? –

+0

나는 후자를하려하고있다. – jvangore31

답변

1

당신은 그들이 컨트롤러의 컨텍스트에서 호출하는 actions 해시 내부 액션을 정의 할 경우에도. 따라서 this은 컨트롤러이며 동작 해시는 아닙니다.

this.shuffle이 작동하지 않는 이유는 다음과 같습니다. shuffle이 컨트롤러에 있지만 actions 해시에 정의되어 있기 때문입니다.

this.actions.shuffle(); 

그러나 다음 shuffle 당신의 행동 this 상황이 잘못된 것을 :

는 무엇을 할 것인가하는 것은 이것이다 그것은 actions 해시가 아닌 컨트롤러 될 것이다. 따라서 이것을 무시해야합니다 :

this.actions.shuffle.call(this); 

그러나 이것은 약간 흉한 것입니까? 질문에 대한 답변 :

또는 다른 작업에서 사용하지 않을 작업입니까?

예! 이 작업을 수행하려면 이 아닌이 아니라 동일한 controller.js 파일 내에서 정상적인 기능을 호출하거나 컨트롤러에서 정의 된 일반적인 방법을 사용하는 것이 좋습니다. 그래서이 작업을 수행 :

import Ember from 'ember'; 

function foo(arg) { 
    ... 
    return something; 
} 

export default Ember.Controller.extend({ 
    actions: { 
    bar() { 
     let baz = foo(this.things); 
     ... 
    } 
    } 
} 

또는이 :

import Ember from 'ember'; 

function foo(arg) { 
    ... 
    return something; 
} 

export default Ember.Controller.extend({ 
    foo(arg) { 
    ... 
    return something; 
    }, 
    actions: { 
    bar() { 
     let baz = this.foo(this.things); 
     ... 
    } 
    } 
} 

actions 해시에 대한 아이디어는 템플릿 (actions) 정상적인 방법으로 호출되는 함수 사이를 분리하는 것입니다.

+0

감사합니다. 형, 많이 도움이되었습니다. – jvangore31

+0

이것은 당신이 성취하려는 것을 발견 한 후에 제가 제안하려고했던 것입니다. 잘 했어. :) –

관련 문제