Last active
June 29, 2017 09:59
-
-
Save far-blue/a652029beaaf9b7a97455c0d9cdd8678 to your computer and use it in GitHub Desktop.
ko observable extender to support Observable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<div id="app"> | |
<input data-bind="textInput: x" class="x"> + <input data-bind="textInput: y" class="y"> = <span data-bind="text: result" class="result"></span> | |
</div> | |
<script src="most.js"></script> | |
<script src="knockout.js"></script> | |
<script> | |
ko.extenders.Observable = function (f) { | |
f[ko.extenders.Observable.symbolCompat()] = ko.extenders.Observable.observable; | |
return f; | |
} | |
ko.extenders.Observable.symbolCompat = function() { | |
return (typeof Symbol === 'function' ? (Symbol.observable ? Symbol.observable : Symbol('observable')) : '@@observable'); | |
} | |
ko.extenders.Observable.observable = function () { | |
return { subscribe: ko.extenders.Observable.subscription.bind(this) } | |
} | |
ko.extenders.Observable.subscription = function (thingObserving) { | |
return (this.subscribe(function(newValue) {thingObserving.next(newValue);})).dispose; | |
} | |
ko.extenders.Observer = function (f) { | |
f.next = f; | |
return f; | |
} | |
var App = function () { | |
this.x = ko.observable().extend({Observable: true}); | |
this.y = ko.observable().extend({Observable: true}); | |
this.result = ko.observable().extend({Observer: true}); | |
most.combine( | |
function (x, y) { return x + y }, | |
most.from(this.x).map(Number.parseInt), | |
most.from(this.y).map(Number.parseInt) | |
).subscribe(this.result); | |
}; | |
ko.applyBindings(new App(), document.getElementById('app')); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment