Instead of instanceof
use toString
to solve cross-frame/context issues
#1307
Milestone
instanceof
use toString
to solve cross-frame/context issues
#1307
So I use jsdom to run my binding handlers unit tests. And I realized checked binding is failing to recognize that the value of the binding is an array but it was treating it as a boolean, so checked binding was checking all my checkboxes instead of one. I checked knockout source and found the problem was coming from this line:
ko.utils.unwrapObservable(valueAccessor()) instanceof Array
,The problem is that my unit tests is in one context(the main nodejs script context) and knockoutjs source script is evaluated in another context(done by jsdom). And the
Array
constructor is different in both contexts. Same issue happens when if the array came from another frame in the browser.This problem has been described here: http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
To replicate the problem on browser you can try this:
As a fix, we can add a util function called isArray:
But arrays are not the only things that are checked with instanceof. I found this other checking if object is primitive or not:
knockout/src/subscribables/mappingHelpers.js
Line 27 in c59104c
and also somebody asked for this before: #184 (comment)
Note that most of popular utilities libraries(underscore, lodash, sugar, jquery) use this method to check the type.
The text was updated successfully, but these errors were encountered: