Knockout.js如何使用ViewModel函数间接调用applybindings [英] knockoutjs how to call applybindings with viewmodel function indirectly

查看:70
本文介绍了Knockout.js如何使用ViewModel函数间接调用applybindings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的问题,但我只是不知道该怎么做或找到正确的答案.我正在尝试实现changeTracker 此处,但我有一个viewModel函数,而不仅仅是一个变量.我目前正在使用

This should be an easy question, but I just can't figure out how to do it or find the right answer. I'm trying to implement the changeTracker here but I have a viewModel function, not just a variable. I'm currently using

var viewModel = function () {
    // code
}

并使用

var vm = new viewModel();
ko.applyBindings(vm);

如何将changeTracker集成到我的应用程序中?我尝试过

How do I incorporate the changeTracker into my application? I tried

var vm = new changeTracker(viewModel());

var vm = new changeTracker(new viewModel());

,但都不起作用. changeTracker小提琴html是:

but neither works. The changeTracker fiddle html is:

<p>Text property: <input data-bind="value: someTextProperty" /></p>
<p>Bool property: <input type="checkbox" data-bind="checked: boolProperty" /></p>
<p>Array items count: <span data-bind="text: arrayItems().length"></span> <button data-bind="click:addItem">Add</button></p>

<hr/>

<p>Something has changed? <b data-bind="text: tracker().somethingHasChanged() ? 'YES' : 'NO'"></b></p>
<button data-bind="click: tracker().markCurrentStateAsClean, enable: tracker().somethingHasChanged">
    Mark current state as clean
</button>

而javascript是:

and the javascript is:

function changeTracker(objectToTrack, hashFunction) {    
    hashFunction = hashFunction || ko.toJSON;
    var lastCleanState = ko.observable(hashFunction(objectToTrack));

    var result = {
        somethingHasChanged : ko.dependentObservable(function() {
            return hashFunction(objectToTrack) != lastCleanState()
        }),
        markCurrentStateAsClean : function() {
            lastCleanState(hashFunction(objectToTrack));   
        }
    };

    return function() { return result }
}

var viewModel = {
    someTextProperty: ko.observable("Hello"),
    boolProperty: ko.observable(false),
    arrayItems: ko.observableArray([]),

    addItem : function() { this.arrayItems.push("Another") }
};
viewModel.tracker = new changeTracker(viewModel);
ko.applyBindings(viewModel);

推荐答案

我更改了jsFiddle,它使用了构造函数,并且工作正常.您将changeTracker附加到viewModel,而不使用作为绑定的viewModel返回的changeTracker实例.

I changed the jsFiddle is use a constructor function and it works without issue. You attach changeTracker to the viewModel, you don't use the changeTracker instance returned as the bound viewModel.

var ViewModel = function() {
    this.someTextProperty = ko.observable("Hello");
    this.boolProperty = ko.observable(false);
    this.arrayItems = ko.observableArray([]);

    this.addItem = function() { this.arrayItems.push("Another"); };
};
var viewModel = new ViewModel();
viewModel.tracker = new changeTracker(viewModel);
ko.applyBindings(viewModel);

这篇关于Knockout.js如何使用ViewModel函数间接调用applybindings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆