2012-08-08 6 views
1

게임용 자바 스크립트 애플리케이션을 만들고 있습니다. 목표는 MMORPG를위한 "빌드 계산기"를 만드는 것입니다. 내 응용 프로그램에 이미 저장된 데이터와보기를 동기화하려고합니다. 내가 검색 한 다른 프레임 워크를 테스트 및 녹아웃-JS는다른 객체와 데이터 바인딩 내부의 녹아웃, ViewModel

html로 저를위한 최적의 솔루션이 될 것처럼 보였다 :

<script> 
var simulatorTool = new SimulatorTool(); 
</script> 

<body> 
<p data-bind="text: test"> </p> 
<p data-bind="text: test2"> </p> 
</body> 

자바 스크립트 도구 :

function SimulatorTool() 
{ 

meSim = this; 

this.config = new Config(); 
this.data = new Data(); 
this.stuff = new Stuff(); 
this.traits = new Traits(); 
this.view = new AppViewModel(); 
$(function(){ 
    ko.applyBindings(meSim.view); 
}); 

... functions 

} 

보기 모델 :

function AppViewModel() { 
    this.test = "Test!"; 

    this.test2 = ko.computed(function() { 
     return meSim.data.selectedData['profession'] 
    }, this); 
} 

문제는 다른 개체의 뷰에 html을 바인딩하는 방법을 모르겠다는 것입니다. 나는 그것이 가능한지조차 모른다. 뷰는 응용 프로그램에서 분리되어 있다면 어떻게 "SimulatorTool"내부의 데이터에 액세스 할 수없는 경우에는 그

<p data-bind="simulatorTool.view, text: test"> </p> 

그런 짓을하고 싶습니다?

답변

-1

나는 싶은 것은이 같은 생각 -의이 simulator_tool.js에 가정 해 봅시다 :

var SimulatorTool = SimulatorTool || {}; 

SimulatorTool.ViewModel = function(initialData){ 
    //bindings etc 
}; 

SimulatorTool.Start - function(initialData){ 
    var viewModel = SimulatorTool.ViewModel(initialData); 
    ko.applyBindings(viewModel); 
    return viewModel //if you want to play with it in the console... 
}; 

그런 다음 페이지 :

<script> 
$().ready(function()){ 

    var payload = @Html.Raw(ViewBag.SimulatorJSON); 
    SimulatorTool.Start(payload); 

} 
</script> 

가 이상적으로 응용 프로그램을 할 수도 있습니다. js 파일에서 SimulatorTool 네임 스페이스가 이미 설정되어 있으므로 선언을 제거 할 수 있습니다. JSON 덤프가 이미 config를 설정했는지 또는 다른 곳에 설정되어 있는지 여부는 알 수 없지만 일반적으로이 함수는 함수 또는 기타 등을 사용하여 자체 네임 스페이스에 속합니다.

관련 문제