2013-01-10 4 views
0

콜백에서 이것을 반환하려했지만 항상 정의되지 않습니다. 여기 javascript chainning이 콜백에서 이것을 반환합니다.

create: function(currentView, data){ 
    var itsMe = this; 
    this.thumbsWrapper = this.templates.wrapper().hide(); 
    currentView.append(this.thumbsWrapper); 
    this.thumbsWrapper.fadeIn("fast", function(){ 
     return itsMe;             
    }); 
}, 

var l = list().create(currentView); //need teh return that i can use chaining 

var에 리터를 냈다입니다 지금 정의되지 않은, 나는 그것이 OBJ

에게 반환하는 콜백 fadeIn를 사용 해달라고하면 콜백 ... 로 fadeIn를 사용하는 경우 누구 아이디어? @Felix 클링가 올바른지 뭐라고, 당신은 아무것도 반환하지 않습니다

create: function(currentView, data){ 
    var itsMe = this; 
    this.thumbsWrapper = this.templates.wrapper().hide(); 
    currentView.append(this.thumbsWrapper); 
    this.thumbsWrapper.fadeIn("fast", function(){ 
     return itsMe; //<--- this isn't going anywhere because you don't capture it            
    }); 
    return itsMe; //<------ return the object 
}, 
+0

콜백은 호출 된 곳의 값을 반환하기 때문에'.fadeIn' 내부에 있습니다. 'create' 함수에는 아무런 영향을 미치지 않습니다. –

답변

1

당신은 create() 함수에서 객체를 반환해야, 그것은 현재 아무것도 반환하지 않습니다. 당신이 itsMe을 반환 할 경우에 당신은 수행해야합니다

create: function(currentView, data){ 
    var itsMe = this; 
    this.thumbsWrapper = this.templates.wrapper().hide(); 
    currentView.append(this.thumbsWrapper); 
    this.thumbsWrapper.fadeIn("fast"); 
    return itsMe;  
} 

당신이 체인 원하는 경우 충분해야한다.

당신이 페이드 아웃이 완료되면 itsMe에 대한 참조를 얻고 싶은 경우에, 당신이 당신의 자신의 콜백 통과해야합니다

create: function(currentView, data, callback){ 
    var itsMe = this; 
    this.thumbsWrapper = this.templates.wrapper().hide(); 
    currentView.append(this.thumbsWrapper); 
    this.thumbsWrapper.fadeIn("fast", function(){ 
     callback(itsMe); 
    }); 
} 


list().create(function (that) { 
    console.log("fade out complete"); 
    console.log("itsMe is", that); 
}); 

그리고 당신은 다음을 수행 할하는 체인 패턴을 갖고 싶어 함수가 체인에서 fadeout이 끝나면 this에 대한 참조가 아닌 명령을 대기열에 넣을 수있는 객체를 전달해야하며 각 명령을 순차적으로 구현해야합니다.

+0

또한 애니메이션 함수의 비동기 특성으로 인해이 함수는 가능한 빨리 *를 반환합니다. 즉, 페이드 인 애니메이션이 애니메이션 대기열에 추가되면 바로 *가 반환됩니다. –

+0

@MattiasBuelens는 사실이지만 아무것도하지 않거나 fadeIn 완료 호출에서 itsMe를 수정하지 않았으므로 OK입니다. – MrCode

+0

동의했다. OP가 'create'함수가 반환 할 것으로 예상 할 때 명확하지 않았기 때문에 * 주목했다. –

관련 문제