Add cross-frame compatibility to knockout.mapping #167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was using knockout.mapping on a project where the plugin needed to be loaded within a sandboxed environment (in this case, an iframe) and noticed that it was in no way behaving as expected. This seemed to be due to two things:
exports.getType
was looking at an object's constructor to determine what kind of object it was.instanceof
operator for type inference.Because of these, the library would not work unless loaded within the same window context as the script in which it was being used. This PR fixes that issue by using
Object.prototype.toString
to find the internal[[Class]]
property of the object (as per the ECMA spec) and return the proper value from there.Note that with the current implementation
getType
will return "object" when passednull
. I'm not sure if this is the behavior that you're looking for since in most cases a client doesn't treat a "null" property the same way it treats other objects, but I left it since it's outside the scope of this PR. Happy to add it though if you guys would like! I tried it out and ran the QUnit suite and everything still passed.p.s. AWESOME knockout plugin! Made my life a whole lot easier for this project!