2012-01-31 7 views
1

** 업데이트 됨녹아웃 JS를 구성하는 방법 Jquery App

나는 jquery와 함께 knockoutjs를 사용하여 응용 프로그램을 작성하고 있습니다.

jquery를 사용하고 있기 때문에 jquery 함수에서 knockout을 래핑해야합니다. 이것은 응용 프로그램을 통해 여러 다른 페이지에 대해 동일한 객체/기능을 다시 사용한다는 점을 제외하고는 괜찮습니다. 객체가 녹아웃에 대한 참조를 포함하기 때문에 전역 파일에 넣을 수 없기 때문에 반복 횟수가 많아서 코드가 심각하게 커지고 관리하기가 어렵습니다.

그래서

//global file 

//this is fine because it dose not reference knockout 
var global_cust = function(){ 
    this.name: "dave"; 
    this.isAwkward: true; 
} 

//this is not fine because it references knockout 
var knockout_global_customer = function(properties){ 
    this.name: ko.observable(properties.name? properties.name: "unknown"), 
    this.isAwkward: ko.observable(properties.isAwkward? properties.isAwkward: true), 
} 

//page 1 

(function($){ 

//i have to include the customer in every page of aplplication that uses it. I would like this in a global file or someway of Precompiling it so don't have to include it in every page - a bit like php include but for JS  
var customer = function(properties){ 
    this.name: ko.observable(properties.name? properties.name: "unknown"), 
    this.isAwkward: ko.observable(properties.isAwkward? properties.isAwkward: true), 
} 

var viewModel = { 
    currentCustomer: ko.observable(new customer()), 
    globalCustomer: ko.observable(new global_cust()), 
} 

ko.applyBindings(viewModel); 

})(jQuery); 

그래서 어쨌든, 또는 내가하지 않도록 사전 프로세스 파일 도구는 모든 페이지에

감사

+0

왜 외부의 뷰 모델에 액세스해야합니까? – AlfeG

+0

귀하의 의견을 주셔서 감사합니다, 당신은 세계에 의해 무슨 뜻인지 모르겠지만 viewModel 내가 볼 필요가 글로벌 그것은보기 모델이 참조하는 고객 기능입니다. 내 애플 리케이션의 각 페이지마다 다른 속성을 포함하는 다른 viewmodel을 가지고, 그 중 일부는 객체/기능을 참조합니다. – Rob

답변

1
을 동일한 개체/기능을 복제 유지

이 목적으로 변수를 사용할 수 있습니다.

window.customerModel = viewModel; 

전 세계에서 사용할 수 있습니다.

0

저는 윈도우 변수 응답을 좋아하며 가장 적합하다고 생각합니다. 그러나 "php include"와 같은 기능을 사용하려면 기능을 확인하십시오. http://api.jquery.com/jQuery.getScript/

+0

페이지를로드 한 후 여러 번 반복되는 코드로 인해 js가 부풀어 오르지 않습니까? – AlfeG

+1

내가 말했듯이, 나는 이것이 그를위한 최선의 해결책이라고 생각하지 않지만 그의 포함 질문과 관련이 있습니다. 나는 심지어 다른 남자를 upvoted. ;) –

관련 문제