For when you need a widget interface
Created
August 27, 2012 02:17
-
-
Save Raynos/3485064 to your computer and use it in GitHub Desktop.
Widget
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
var sorta = require("sorta") | |
, Element = require("fragment").Element | |
, string = "<span data-foo></span>" | |
, DeltaStream = require("delta-stream") | |
, databind = require("data-bind") | |
, SpliceStream = require("splice-stream") | |
var spliceStream = SpliceStream() | |
, list = spliceStream.createList() | |
, deltaStream = DeltaStream() | |
, delta = deltaStream.createDelta() | |
, sort = sorta({ list: "ul", item: "li" }, createWidget) | |
spliceStream.pipe(sort) | |
list.push(deltaStream) | |
delta.set("foo", "bar") | |
sort.appendTo(document.body) | |
/* | |
<body> | |
<ul> | |
<li> | |
<span data-foo>bar</span> | |
</li> | |
</ul> | |
</body> | |
*/ | |
function createWidget(stream, element) { | |
var elem = Element(string) | |
, widget = Widget(elem) | |
stream.pipe(databind(elem)) | |
widget.appendTo(element) | |
} |
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
module.exports = Widget | |
function Widget(elem) { | |
return { | |
appendTo: appendTo | |
} | |
function appendTo(parent) { | |
parent.appendChild(elem) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment