使用ko.viewmodel插件更新模型

时间:2014-09-03 13:58:53

标签: jquery knockout.js knockout-mapping-plugin

似乎无法从jquery方面调用“ko.viewmodel”淘汰相关内容

错误:

TypeError: ko.viewmodel is undefined  

viewModel = ko.viewmodel.updateFromModel(result);

我正在使用的内容:

使用它进行映射,可以生成更清晰的代码:

coderenaissance.github.io/knockout.viewmodel/

首先所有的代码都在Jquery方面,我的意思是当我使用Mapping插件时在这个函数里面:

//var vmProcess = $(function () {
//});";

我从中删除了整个代码,因为从第一行使用ko.viewmodel获取了未定义的错误

当我切换到ko.viewmodel时,我发现我将所有东西都移到了着名的$之外,但在那里我无法按常规方式更新我的模型,  不知道你有解决方案吗? 提前谢谢......

守则:

var viewModel = ko.viewmodel.fromModel(serverViewModel);

//function updateModel(result) {
//    console.log('updating model');
//    viewModel = ko.viewmodel.updateFromModel(result);
//}

viewModel.addGroup = function(newgroupName) {
    console.log("Adding Group... ");
    $.ajax({
        url: "/Admin/AddGroup?groupName=" + newgroupName,
        contentType: 'application/json;charset=utf-8',
        dataType: 'JSON',
        type: "POST",
        success: function(result) {
            //updateModel(result);
            ko.viewmodel.updateFromModel(viewModel,result);
        }
    });
}

//var vmProcess = $(function () {
//});

ko.applyBindings(viewModel);

修改 添加完整代码

<script src="~/Scripts/jquery.js"></script>
<script src="~/Scripts/knockout.js"></script>
<script src="~/Scripts/knockout.viewmodel.2.0.3.js"></script>

<script type="text/javascript">
    var serverViewModel = @Html.Raw(Json.Encode(Model));

    var viewModel = ko.viewmodel.fromModel(serverViewModel);
    var self = this;
    viewModel.NewGroupName = '';

    viewModel.addGroup = function(newgroupName) {
        $.ajax({
            url: "/Admin/AddGroup?groupName=" + newgroupName,
            contentType: 'application/json;charset=utf-8',
            dataType: 'JSON',
            type: "POST",
            success: function(result) {
                ko.viewmodel.updateFromModel(viewModel,result);
            }
        });
    }

    //var vmProcess = $(function () {
    //});

    ko.applyBindings(viewModel, document.getElementById("pnlSetTournament"));

</script>

0 个答案:

没有答案