Skip to content

Instantly share code, notes, and snippets.

@Raynos

Raynos/blargh.js Secret

Created August 22, 2012 07:25
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 Raynos/11d6cb7218ef3258af77 to your computer and use it in GitHub Desktop.
Save Raynos/11d6cb7218ef3258af77 to your computer and use it in GitHub Desktop.
var set = Set()
, sorted = sortable()
set.on("add", function (obj) {
var widget = Widget(obj, sorted())
})
function Widget(obj, place) {
var elem = makeElement()
, widget = new EventEmitter()
databind(elem, obj)
place.append(elem)
obj.on("end", function () {
place.destroy()
})
obj.on("changes", function (changes) {
if (changes.value) {
place.updateValue(changes.value)
}
})
widget.value = function () {
return obj.get("value")
}
place.updateValue(widget.value())
return widget
}
function sortable() {
var sort = sorta({
compare: function (a, b) {
return a.value < b.value ? 1 : -1
}
}, function (row) {
var key = row.key
, value = row.value
, emitter = value.emitter
emitter.on("value", function (v) {
value.value = v
row.update(value)
})
emitter.on("destroy", function () {
row.update(undefined)
})
return value.element
})
return Place
function Place() {
var value
, elem
, emitter = new EventEmitter()
return {
updateValue: function (v) {
value = v
emitter.emit("value", v)
}
, append: function (e) {
elem = e
write()
}
, destroy: function () {
emitter.emit("destroy")
}
}
function write() {
sort.write(uuid(), {
element: elem
, value: value
, emitter: emitter
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment