Javascript 敲除、其他对象内的ViewModel和数据绑定
我正在为一个游戏做一个javascript应用程序,目标是为MMORPG制作一个“构建计算器”。我想将视图与应用程序中已存储的数据同步。我搜索并测试了不同的框架,knockout js似乎是我最好的解决方案 Html: 视图模型:Javascript 敲除、其他对象内的ViewModel和数据绑定,javascript,data-binding,knockout.js,Javascript,Data Binding,Knockout.js,我正在为一个游戏做一个javascript应用程序,目标是为MMORPG制作一个“构建计算器”。我想将视图与应用程序中已存储的数据同步。我搜索并测试了不同的框架,knockout js似乎是我最好的解决方案 Html: 视图模型: function AppViewModel() { this.test = "Test!"; this.test2 = ko.computed(function() { return meSim.data.selectedData['
function AppViewModel() {
this.test = "Test!";
this.test2 = ko.computed(function() {
return meSim.data.selectedData['profession']
}, this);
}
问题是我不知道如何将html绑定到其他对象中的视图。我甚至不知道这是否可能。我想做那样的事
<p data-bind="simulatorTool.view, text: test"> </p>
如果没有,如果视图与应用程序分离,我如何访问“simulator工具”中的数据?我认为您要做的是这样的事情-比如在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>
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>
SimulatorTool.Config = {
}
SimulatorTool.Stuff = {
}
//etc