forked from weepy/o_O
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
partial rewrite. More modular. Added o_O.object. Better testing
- Loading branch information
Showing
31 changed files
with
1,493 additions
and
9,903 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
TESTS = test/*.js | ||
UGLIFY = $(shell find node_modules -name "uglifyjs" -type f) | ||
|
||
all: o_O.min.js | ||
|
||
test: | ||
@./node_modules/.bin/mocha \ | ||
$(TESTS) | ||
|
||
o_O.js: | ||
@node bin/build.js $^ | ||
|
||
o_O.min.js: o_O.js | ||
@$(UGLIFY) $< > $@ \ | ||
&& du o_O.min.js \ | ||
&& du o_O.js | ||
|
||
clean: | ||
rm -f o_O.js | ||
rm -f o_O.min.js | ||
|
||
.PHONY: test clean |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var files = ["header", "o_O", "eventize", "property", "binding", "helpers", "bindings", "klass", "collection"] | ||
var fs = require("fs") | ||
|
||
var js = files.map(function(file) { | ||
return fs.readFileSync("./lib/" + file + ".js") | ||
}) | ||
|
||
fs.writeFileSync("o_O.js", "!function() {\n\n" + js.join("\n\n") + "\n\n}();") |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
o_O.expression = function(text) { | ||
o_O.expression.last = text // useful for catching syntax errors if this breaks | ||
return new Function('o_O', 'with(this) { { return (' + text + ')} } ') | ||
} | ||
|
||
|
||
/* | ||
* calculates the dependencies | ||
* calls the callback with the result | ||
* if running fn returns a function - nothing more happens | ||
* otherwise the callback is called with the function result everytime a dependency changes | ||
*/ | ||
|
||
o_O.bindFunction = function(fn, callback) { | ||
var deps = o_O.dependencies(fn) | ||
var result = o_O.dependencies.lastResult | ||
var isEvent = typeof result == 'function' | ||
callback(result) | ||
|
||
// if this is an event watch for changes and reapply | ||
if(!isEvent) { | ||
forEach(deps, function(dep) { | ||
dep.on('set', function(value) { | ||
callback(fn()) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
|
||
o_O.bindElementToRule = function(el, attr, expr, context) { | ||
var expression = o_O.expression(expr) | ||
|
||
var trigger = function() { | ||
return expression.call(context, o_O.helpers) | ||
} | ||
|
||
o_O.bindFunction(trigger, function(x) { | ||
var y = typeof x == 'function' && !o_O.property.is(x) | ||
? function(e) { return x.call(context, e) } | ||
: x | ||
return o_O.bindings[attr].call(context, y, $(el)) | ||
}) | ||
|
||
} | ||
|
||
|
||
|
||
function extractRules (str) { | ||
var rules = trim(str).split(";") | ||
var ret = [] | ||
for(var i=0; i <rules.length; i++) { | ||
var rule = rules[i] | ||
rule = trim(rule) | ||
if(!rule) continue // for trailing ; | ||
var bits = trim(rule).split(":").map(trim) | ||
var attr = trim(bits.shift()) | ||
var param = trim(bits.join(":")) | ||
|
||
ret.push([attr, param]) | ||
} | ||
return ret | ||
} | ||
|
||
o_O.bind = function(context, dom) { | ||
var $el = $(dom) | ||
context.el = $el[0] | ||
|
||
function bind(el, k, v) { | ||
var $$ = $(el) | ||
|
||
var rules = extractRules($$.attr("bind")) | ||
|
||
for(var i=0; i <rules.length; i++) { | ||
var rule = rules[i] | ||
o_O.bindElementToRule($$, rule[0], rule[1], context) | ||
} | ||
$$.attr("bind",null) | ||
} | ||
|
||
// lets do foreach first | ||
$(dom).filter("[bind^=foreach]").each(function(k, v) { | ||
bind(this, k, v) | ||
}) | ||
|
||
$(dom).find("[bind^=foreach]").each(function(k, v) { | ||
bind(this, k, v) | ||
}) | ||
|
||
$(dom).filter("[bind]").each(function(k, v) { | ||
bind(this, k, v) | ||
}) | ||
|
||
$(dom).find("[bind]").each(function(k, v) { | ||
bind(this, k, v) | ||
}) | ||
|
||
} |
Oops, something went wrong.