0

例如,

  1. 我有 KO 已经注册的页面,并且有一个带有可观察属性“someProperty”的视图模型;
  2. 我检查“someProperty”是否是 ko.isObservable(viewmodel.someProperty) 的可观察属性 - 它返回“true”;
  3. 我进行 ajax 调用以获取一些注册 KO 的 html 标记;
  4. 现在,如果您检查 ko.isObservable(viewmodel.someProperty) 它将返回 false;

此外,所有手动添加的 KO 扩展都将丢失。它看起来像 jQuery ( http://bugs.jquery.com/ticket/10066 ) 中的错误(或功能)。

var viewModel = new function() {
            var self = this;
            this.serverData = {
                Controller: ko.observable(null),
                Enabled: ko.observable(false),
                Id: ko.observable(null),
                ParentId: ko.observable(null),
                Title: ko.observable(null),
                MaterialId: ko.observable(null),
                Alias: ko.observable(null)
            };
            this.treeData = {
                tree: ko.observable(null),
                node: ko.observable(null)
            };
            this.submit = submit;
            this.cancel = cancel;
            this.openMaterials = menuOptions.openMaterials;
}

// ...
var data = ko.utils.createUnobservable(viewModel.serverData);
// ...

(function(ko) {
    ko.utils = ko.utils || {};

    ko.utils.createUnobservable = function(observable) {
        var unobservable = {};
        (function() {
            for (var propertyName in observable) {
                var observableProperty = observable[propertyName];
                if (ko.isObservable(observableProperty) /* always 'false' after ajax */) {
                    unobservable[propertyName] = observableProperty();
                }
            }
        })();

        return unobservable;
    };

})(ko = ko || {});
4

1 回答 1

0

You could fix this by saving a copy of the ko global variable before you include the loaded ajax, and then restoring it afterwards.

var savedKo = window.ko;
.... // do the ajax thing
window.ko = savedKo;
于 2013-03-05T20:42:16.320 回答