2012-06-20 5 views
0

어떻게이 자바 스크립트를 coffeescript로 바꿀 수 있습니까?knockoutjs javascript to coffeescript

다음
jQuery -> 
    AppViewModel = -> 
    this.firstName = ko.observable("something") 

    ko.applyBindings(new AppViewModel()) 

는 커피 스크립트가의 한 부분으로 일) 당신이 ko.applyBindings을 만드는 커피 스크립트 버전 (에서

(function() { 

    jQuery(function() { 
    var ViewModel; 
    ViewModel = function() { 
     return this.firstName = ko.observable(); 
    }; 
    return ko.applyBindings(new ViewModel()); 
    }); 

}).call(this); 

답변

4

이것은 트릭을 만들었습니다. 나는이에 나이를 보냈다 knockoutjs

jQuery -> 
    class AppViewModel 
    firstName : ko.observable() 

    ko.applyBindings(new AppViewModel) 
+0

gah, 맞아요. 들여 쓰기 잘못됨. – Brand

0

을 생산 무엇

$(function() { 
    function AppViewModel() { 
    this.firstName = ko.observable(); 
    } 

    ko.applyBindings(new AppViewModel()); 
}); 

나는이 시도하지만 knockoutjs 바인딩을 나누기 원래 JavaScript에서는 ko.applyBindings()가 외부에서 발생했지만 jQuery 문서는 준비가되어있다.

ko.applyBindings (새 ​​AppViewModel()) 행을 왼쪽 끝까지 이동해보십시오.

원래 코드와 새 JavaScript에서 생성 된 JavaScript를 보면이 효과를 볼 수 있습니다.

+0

죄송합니다. 그 코드의 오타가 있습니다 :( – Brand

+0

아직 생성 된 CoffeeScript와 원본 JavaScript의 차이점이 있습니까? –

+0

사실 예! Coffeescript는'this.firstName' 및' ko.applyBindings'. 그걸 막기위한 어떤 방법이 있습니까? – Brand

1

CoffeeScript에서 개체를 처리하는 방법이 상당히 다릅니다. 좋은 참조 http://arcturo.github.com/library/coffeescript/index.html :

jQuery -> 
    AppViewModel = 
    firstName: ko.observable() 

    ko.applyBindings(new AppViewModel()) 

체크 아웃 : 당신은 아마 같은 일을해야합니다.

+1

고맙습니다. 그러나 이것은 작동하지 않았습니다. – Brand

1

을 배울 때 커피 스크립트를 사용하지 않는 -하지만 실제 솔루션입니다 생각합니다. 소개 방법 (원래의 게시물)과 비슷한 것을 위에서 "클래스"로 바꿨다가 다시 돌아 왔습니다. 무엇을 깨는 것은 커피 스크립트에 의해 생성 된 반환하고 정말 간단한 해결책이있다 :

$ -> 
    AppViewModel = -> 
     @firstname = ko.observable 'andrew' 
     @lastname = ko.observable 'plumdog' 
     @fullname = ko.computed => 
      @firstname() + ' ' + @lastname() 
     @ 

명시 적으로 마지막에 @를 반환함으로써이 모든 반환은 고정되어 있습니다. 나는 원래의 질문보다 2 줄 많은 것을 포함 시켰습니다. =는 계산의 결과를 원래 함수의 맥락에서 실행해야하므로 사용하십시오.