Skip to content

Instantly share code, notes, and snippets.

@kmnk
Created December 9, 2012 17:36
Show Gist options
  • Save kmnk/4246228 to your computer and use it in GitHub Desktop.
Save kmnk/4246228 to your computer and use it in GitHub Desktop.
Compiled Sample CoffeeScript
Namespace('sample.widget').use('sample.rect.ui createRectModel').define(function(ns) {
var registerElement;
registerElement = function(element, dataset) {
var rectModel;
rectModel = ns.createRectModel(element, dataset.classOn, dataset.classOff);
return element.addEventListener('click', function(event) {
rectModel.notify('toggle').run();
return rectModel.notify('move').run();
});
};
return ns.provide({
registerElement: registerElement
});
});
Namespace('sample.rect.ui').use('brook promise').use('brook.util mapper,through').use('brook.model createModel').define(function(ns) {
var createModel, createMovePositionPromise, createToggleClassPromise, makeNewPosition, maxLeft, maxTop;
maxLeft = 200;
maxTop = 200;
createModel = function(element, onClass, offClass) {
var model;
model = ns.createModel();
model.addMethod('toggle', ns.promise().bind(createToggleClassPromise(element, onClass, offClass)));
model.addMethod('move', ns.promise().bind(makeNewPosition, createMovePositionPromise(element)));
return model;
};
createToggleClassPromise = function(element, onClass, offClass) {
return ns.through(function(v) {
if (element.className === onClass) {
return element.className = offClass;
} else {
return element.className = onClass;
}
});
};
createMovePositionPromise = function(element) {
return ns.through(function(v) {
element.style.left = "" + v.left + "px";
return element.style.top = "" + v.top + "px";
});
};
makeNewPosition = ns.mapper(function(v) {
return {
left: Math.floor(Math.random() * maxLeft),
top: Math.floor(Math.random() * maxTop)
};
});
return ns.provide({
createRectModel: createModel
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment