Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cross-scope/iframe support #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 25 additions & 18 deletions build/output/knockout-latest.debug.js
Expand Up @@ -2,7 +2,7 @@
// (c) Steven Sanderson - http://knockoutjs.com/
// License: MIT (http://www.opensource.org/licenses/mit-license.php)

(function(window,undefined){
(function(window,undefined){
var ko = window["ko"] = {};
// Google Closure Compiler helpers (used only to make the minified file smaller)
ko.exportSymbol = function(publicPath, object) {
Expand Down Expand Up @@ -172,9 +172,20 @@ ko.utils = new (function () {
return false;
},

domNodeIsAttachedToDocument: function (node) {
return ko.utils.domNodeIsContainedBy(node, document);
},
domNodeIsAttachedToDocument: function(node) {
var curr = node;
while (true) {
curr = curr.parentNode;
// If parent is null or DOCUMENT_FRAGMENT_NODE return false
if (null == curr || 11 == curr.nodeType ) {
return false;
}
// If parent is DOCUMENT_NODE return true
if (9 == curr.nodeType) { // DOCUMENT_NODE
return true;
}
}
},

registerEventHandler: function (element, eventType, handler) {
if (typeof jQuery != "undefined") {
Expand Down Expand Up @@ -794,7 +805,10 @@ ko.observable = function (initialValue) {
ko.utils.extend(observable, ko.observable['fn']);

ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);


observable.isKnockoutObservable = true;
observable.isKnockoutWritableObservable = true;

return observable;
}

Expand All @@ -808,19 +822,10 @@ ko.observable['fn'] = {
};

ko.isObservable = function (instance) {
if ((instance === null) || (instance === undefined) || (instance.__ko_proto__ === undefined)) return false;
if (instance.__ko_proto__ === ko.observable) return true;
return ko.isObservable(instance.__ko_proto__); // Walk the prototype chain
return (typeof instance == "function") && instance.isKnockoutObservable;
}
ko.isWriteableObservable = function (instance) {
// Observable
if ((typeof instance == "function") && instance.__ko_proto__ === ko.observable)
return true;
// Writeable dependent observable
if ((typeof instance == "function") && (instance.__ko_proto__ === ko.dependentObservable) && (instance.hasWriteFunction))
return true;
// Anything else
return false;
return (typeof instance == "function") && instance.isKnockoutWritableObservable;
}


Expand Down Expand Up @@ -1037,7 +1042,8 @@ ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunction
}
}
dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; }
dependentObservable.hasWriteFunction = typeof options["write"] === "function";
dependentObservable.isKnockoutObservable = true;
dependentObservable.isKnockoutWritableObservable = typeof options["write"] === "function";
dependentObservable.dispose = function () {
if (disposeWhenNodeIsRemoved)
ko.utils.domNodeDisposal.removeDisposeCallback(disposeWhenNodeIsRemoved, disposeWhenNodeIsRemovedCallback);
Expand All @@ -1064,6 +1070,7 @@ ko.dependentObservable.__ko_proto__ = ko.observable;

ko.exportSymbol('ko.dependentObservable', ko.dependentObservable);
ko.exportSymbol('ko.computed', ko.dependentObservable); // Make "ko.computed" an alias for "ko.dependentObservable"

(function() {
var maxNestedObservableDepth = 10; // Escape the (unlikely) pathalogical case where an observable's current value is itself (or similar reference cycle)

Expand Down Expand Up @@ -3051,4 +3058,4 @@ ko.exportSymbol('ko.nativeTemplateEngine', ko.nativeTemplateEngine);(function()
ko.setTemplateEngine(jqueryTmplTemplateEngineInstance);

ko.exportSymbol('ko.jqueryTmplTemplateEngine', ko.jqueryTmplTemplateEngine);
})();})(window);
})();})(window);
11 changes: 11 additions & 0 deletions spec/domNodeDisposalBehaviors.js
Expand Up @@ -3,6 +3,17 @@ describe('DOM node disposal', {
before_each: function () {
testNode = document.createElement("div");
},

'DOM node should not be attached to document after removal': function () {
value_of(ko.utils.domNodeIsAttachedToDocument(testNode)).should_be(false);

var body = document.getElementsByTagName("BODY")[0];
body.appendChild(testNode);
value_of(ko.utils.domNodeIsAttachedToDocument(testNode)).should_be(true);

body.removeChild(testNode);
value_of(ko.utils.domNodeIsAttachedToDocument(testNode)).should_be(false);
},

'Should run registered disposal callbacks when a node is cleaned': function () {
var didRun = false;
Expand Down
5 changes: 3 additions & 2 deletions src/subscribables/dependentObservable.js
Expand Up @@ -97,7 +97,8 @@ ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunction
}
}
dependentObservable.getDependenciesCount = function () { return _subscriptionsToDependencies.length; }
dependentObservable.hasWriteFunction = typeof options["write"] === "function";
dependentObservable.isKnockoutObservable = true;
dependentObservable.isKnockoutWritableObservable = typeof options["write"] === "function";
dependentObservable.dispose = function () {
if (disposeWhenNodeIsRemoved)
ko.utils.domNodeDisposal.removeDisposeCallback(disposeWhenNodeIsRemoved, disposeWhenNodeIsRemovedCallback);
Expand All @@ -123,4 +124,4 @@ ko.dependentObservable['fn'] = {
ko.dependentObservable.__ko_proto__ = ko.observable;

ko.exportSymbol('ko.dependentObservable', ko.dependentObservable);
ko.exportSymbol('ko.computed', ko.dependentObservable); // Make "ko.computed" an alias for "ko.dependentObservable"
ko.exportSymbol('ko.computed', ko.dependentObservable); // Make "ko.computed" an alias for "ko.dependentObservable"
18 changes: 6 additions & 12 deletions src/subscribables/observable.js
Expand Up @@ -25,7 +25,10 @@ ko.observable = function (initialValue) {
ko.utils.extend(observable, ko.observable['fn']);

ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);


observable.isKnockoutObservable = true;
observable.isKnockoutWritableObservable = true;

return observable;
}

Expand All @@ -39,19 +42,10 @@ ko.observable['fn'] = {
};

ko.isObservable = function (instance) {
if ((instance === null) || (instance === undefined) || (instance.__ko_proto__ === undefined)) return false;
if (instance.__ko_proto__ === ko.observable) return true;
return ko.isObservable(instance.__ko_proto__); // Walk the prototype chain
return (typeof instance == "function") && instance.isKnockoutObservable;
}
ko.isWriteableObservable = function (instance) {
// Observable
if ((typeof instance == "function") && instance.__ko_proto__ === ko.observable)
return true;
// Writeable dependent observable
if ((typeof instance == "function") && (instance.__ko_proto__ === ko.dependentObservable) && (instance.hasWriteFunction))
return true;
// Anything else
return false;
return (typeof instance == "function") && instance.isKnockoutWritableObservable;
}


Expand Down
17 changes: 14 additions & 3 deletions src/utils.js
Expand Up @@ -156,9 +156,20 @@ ko.utils = new (function () {
return false;
},

domNodeIsAttachedToDocument: function (node) {
return ko.utils.domNodeIsContainedBy(node, document);
},
domNodeIsAttachedToDocument: function(node) {
var curr = node;
while (true) {
curr = curr.parentNode;
// If parent is null or DOCUMENT_FRAGMENT_NODE return false
if (null == curr || 11 == curr.nodeType ) {
return false;
}
// If parent is DOCUMENT_NODE return true
if (9 == curr.nodeType) { // DOCUMENT_NODE
return true;
}
}
},

registerEventHandler: function (element, eventType, handler) {
if (typeof jQuery != "undefined") {
Expand Down