Skip to content

Instantly share code, notes, and snippets.

@cwharris
Last active December 17, 2015 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwharris/5551649 to your computer and use it in GitHub Desktop.
Save cwharris/5551649 to your computer and use it in GitHub Desktop.
Stick THIS in your browser and run it.
Rx.Observable.select = function (element, source) {
return source.select(function (value) {
return {
element: element,
value: value
};
});
};
Rx.Observable.prototype.append = function (type) {
return this.selectMany(function (x) {
var element = document.createElement(type);
x.element.appendChild(element);
return x.value
.finallyAction(function () {
x.element.removeChild(element);
})
.select(function (value) {
return {
element: element,
value: value
};
});
});
};
Rx.Observable.prototype.text = function (func) {
return this.doAction(function (x) {
x.element.innerText = func(x.value);
});
};
Rx.Observable.prototype.attr = function (name, func) {
return this.doAction(function (x) {
x.element.setAttribute(name, func(x.value));
});
};
Rx.Observable.prototype.style = function (name, func) {
return this.doAction(function (x) {
var val = func(x.value);
if (typeof val === 'number') { val = val + 'px'; }
x.element.style[name] = val;
});
};
// - Side Effects
var source = Rx.Observable.timer(0, 100).select(function () {
return Rx.Observable.timer(0, 250).take(10).merge(Rx.Observable.never()).select(function () {
return Math.floor(Math.random() * 50);
});
});
Rx.Observable.select(document.body, source)
.append('span')
.text(function (x) { return x; })
.style('paddingBottom', function (x) { return x; })
.style('borderBottomWidth', function (x) { return 50 - x + 1; })
.subscribe();
span {
display: inline-block;
margin: 0px;
border: 5px solid black;
transition: padding 0.1s, border 0.1s;
width: 25px;
text-align: center;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment