Skip to content

Commit

Permalink
Added possibility of overriding notifySubscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSanderson committed Dec 12, 2011
1 parent eef5fd3 commit 2b978e6
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 90 deletions.
13 changes: 6 additions & 7 deletions build/output/knockout-latest.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,6 @@ ko.subscribable = function () {
ko.utils.extend(this, ko.subscribable['fn']);
ko.exportProperty(this, 'subscribe', this.subscribe);
ko.exportProperty(this, 'extend', this.extend);
ko.exportProperty(this, 'notifySubscribers', this.notifySubscribers);
ko.exportProperty(this, 'getSubscriptionsCount', this.getSubscriptionsCount);
}

Expand All @@ -769,7 +768,7 @@ ko.subscribable['fn'] = {
return subscription;
},

notifySubscribers: function (valueToNotify, event) {
"notifySubscribers": function (valueToNotify, event) {
event = event || defaultEvent;
if (this._subscriptions[event]) {
ko.utils.arrayForEach(this._subscriptions[event].slice(0), function (subscription) {
Expand All @@ -795,7 +794,7 @@ ko.subscribable['fn'] = {


ko.isSubscribable = function (instance) {
return typeof instance.subscribe == "function" && typeof instance.notifySubscribers == "function";
return typeof instance.subscribe == "function" && typeof instance["notifySubscribers"] == "function";
};

ko.exportSymbol('ko.subscribable', ko.subscribable);
Expand Down Expand Up @@ -849,8 +848,8 @@ ko.observable = function (initialValue) {
}
}
ko.subscribable.call(observable);
observable.valueHasMutated = function () { observable.notifySubscribers(_latestValue); }
observable.valueWillMutate = function () { observable.notifySubscribers(_latestValue, "beforeChange"); }
observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }
ko.utils.extend(observable, ko.observable['fn']);

ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
Expand Down Expand Up @@ -1082,13 +1081,13 @@ ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunction
});
var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined, it will default to "window" by convention. This might change in the future.
var newValue = options["read"].call(valueForThis);
dependentObservable.notifySubscribers(_latestValue, "beforeChange");
dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
_latestValue = newValue;
} finally {
ko.dependencyDetection.end();
}

dependentObservable.notifySubscribers(_latestValue);
dependentObservable["notifySubscribers"](_latestValue);
_hasBeenEvaluated = true;
}

Expand Down
147 changes: 73 additions & 74 deletions build/output/knockout-latest.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion spec/observableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,21 @@ describe('Observable', {
// undefined vs object - change
instance({ id: 1 });
value_of(notifiedValues.length).should_be(5);
}
},

'Should be possible to replace notifySubscribers with a custom handler': function() {
var instance = new ko.observable(123);
var interceptedNotifications = [];
instance.subscribe(function() { throw new Error("Should not notify subscribers by default once notifySubscribers is overridden") });
instance.notifySubscribers = function(newValue, eventName) {
interceptedNotifications.push({ eventName: eventName || "None", value: newValue });
};
instance(456);

value_of(interceptedNotifications.length).should_be(2);
value_of(interceptedNotifications[0].eventName).should_be("beforeChange");
value_of(interceptedNotifications[1].eventName).should_be("None");
value_of(interceptedNotifications[0].value).should_be(123);
value_of(interceptedNotifications[1].value).should_be(456);
}
});
16 changes: 15 additions & 1 deletion spec/subscribableBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,19 @@ describe('Subscribable', {
instance.subscribe(function() { });
instance.subscribe(function() { }, null, "someSpecificEvent");
value_of(instance.getSubscriptionsCount()).should_be(2);
}
},

'Should be possible to replace notifySubscribers with a custom handler': function() {
var instance = new ko.subscribable();
var interceptedNotifications = [];
instance.subscribe(function() { throw new Error("Should not notify subscribers by default once notifySubscribers is overridden") });
instance.notifySubscribers = function(newValue, eventName) {
interceptedNotifications.push({ eventName: eventName, value: newValue });
};
instance.notifySubscribers(123, "myEvent");

value_of(interceptedNotifications.length).should_be(1);
value_of(interceptedNotifications[0].eventName).should_be("myEvent");
value_of(interceptedNotifications[0].value).should_be(123);
}
});
4 changes: 2 additions & 2 deletions src/subscribables/dependentObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ ko.dependentObservable = function (evaluatorFunctionOrOptions, evaluatorFunction
});
var valueForThis = options["owner"] || evaluatorFunctionTarget; // If undefined, it will default to "window" by convention. This might change in the future.
var newValue = options["read"].call(valueForThis);
dependentObservable.notifySubscribers(_latestValue, "beforeChange");
dependentObservable["notifySubscribers"](_latestValue, "beforeChange");
_latestValue = newValue;
} finally {
ko.dependencyDetection.end();
}

dependentObservable.notifySubscribers(_latestValue);
dependentObservable["notifySubscribers"](_latestValue);
_hasBeenEvaluated = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/subscribables/observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ ko.observable = function (initialValue) {
}
}
ko.subscribable.call(observable);
observable.valueHasMutated = function () { observable.notifySubscribers(_latestValue); }
observable.valueWillMutate = function () { observable.notifySubscribers(_latestValue, "beforeChange"); }
observable.valueHasMutated = function () { observable["notifySubscribers"](_latestValue); }
observable.valueWillMutate = function () { observable["notifySubscribers"](_latestValue, "beforeChange"); }
ko.utils.extend(observable, ko.observable['fn']);

ko.exportProperty(observable, "valueHasMutated", observable.valueHasMutated);
Expand Down
5 changes: 2 additions & 3 deletions src/subscribables/subscribable.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ ko.subscribable = function () {
ko.utils.extend(this, ko.subscribable['fn']);
ko.exportProperty(this, 'subscribe', this.subscribe);
ko.exportProperty(this, 'extend', this.extend);
ko.exportProperty(this, 'notifySubscribers', this.notifySubscribers);
ko.exportProperty(this, 'getSubscriptionsCount', this.getSubscriptionsCount);
}

Expand All @@ -36,7 +35,7 @@ ko.subscribable['fn'] = {
return subscription;
},

notifySubscribers: function (valueToNotify, event) {
"notifySubscribers": function (valueToNotify, event) {
event = event || defaultEvent;
if (this._subscriptions[event]) {
ko.utils.arrayForEach(this._subscriptions[event].slice(0), function (subscription) {
Expand All @@ -62,7 +61,7 @@ ko.subscribable['fn'] = {


ko.isSubscribable = function (instance) {
return typeof instance.subscribe == "function" && typeof instance.notifySubscribers == "function";
return typeof instance.subscribe == "function" && typeof instance["notifySubscribers"] == "function";
};

ko.exportSymbol('ko.subscribable', ko.subscribable);
Expand Down

0 comments on commit 2b978e6

Please sign in to comment.