How to reset knockout bindings in Jasmine test

ぐ巨炮叔叔 提交于 2019-12-06 04:21:15

Your not explicitly applying your bindings to a node. I believe this will therefore apply to your document.body. Which explains your error as your not cleaning the body.

ko.applyBindings(testSubject);

Ref: http://knockoutjs.com/documentation/observables.html

In case you’re wondering what the parameters to ko.applyBindings do,

The first parameter says what view model object you want to use with the declarative bindings it activates

Optionally, you can pass a second parameter to define which part of the document you want to search for data-bind attributes. For example, ko.applyBindings(myViewModel, document.getElementById('someElementId')). This restricts the activation to the element with ID someElementId and its descendants, which is useful if you want to have multiple view models and associate each with a different region of the page.

Useful Function:

// Checks if Element has Already been Bound (Stops Errors Occuring if already bound as element can't be bound multiple times)
var isBound = function (id) {
    if (document.getElementById(id) != null)
        return !!ko.dataFor(document.getElementById(id));
    else
        return false;
};

Answer:

testSubject = new Mobile.Navigation.ThreeStepNavigation.View();
if (isBound("parent-container"))
    ko.cleanNode(document.getElementById('parent-container'));
ko.applyBindings(testSubject, document.getElementById("parent-container"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!