Permalink
Browse files

upgrade

  • Loading branch information...
1 parent 3b4132c commit 6e0ec769e7e8110057f5c159b266d8365963b307 @weepy committed Apr 14, 2012
Showing with 912 additions and 77 deletions.
  1. +103 −77 o_O.js
  2. +809 −0 o_O.js.old
View
180 o_O.js
@@ -14,7 +14,8 @@ a8" "8a 88 88
(c) 2012 by Jonah Fox (weepy), MIT Licensed */
-var slice = Array.prototype.slice
+var VERSION = "0.2.3";
+var slice = Array.prototype.slice;
var Events = {
/*
@@ -125,6 +126,12 @@ var propertyMethods = {
o_O.bind(this, el)
return this
},
+ emitset: function() {
+ if(this._emitting) return // property is already emitting to avoid circular problems
+ this._emitting = true
+ this.emit('set', this.value, this.old_value)
+ delete this._emitting
+ },
timeout: 0,
constructor: o_O // fake this - useful for checking
}
@@ -136,8 +143,7 @@ function o_O(arg, type) {
if(arguments.length) {
prop.old_value = prop.value
prop.value = simple ? v : arg(v)
- prop.emit('setsync', prop.value, prop.old_value)
- emitProperty(prop)
+ prop.emitset()
} else {
if(dependencies.checking)
dependencies.emit('get', prop) // emit to dependency checker
@@ -152,7 +158,7 @@ function o_O(arg, type) {
else
each(dependencies(prop), function(dep) {
dep.change(function() {
- emitProperty(prop)
+ prop.emitset()
})
})
@@ -162,41 +168,6 @@ function o_O(arg, type) {
}
/*
- * Simple registry which emits all property change from one event loop in the next
- */
-var emitProperty = (function() {
- var list = [],
- timer = null
-
- function run() {
- for(var i=0; i < list.length; i++) {
- var prop = list[i]
- prop.emit('set', prop.value, prop.old_value)
- }
- for(var i=0; i < list.length; i++)
- delete list[i]._emitting
-
- timer = null
- list = []
- }
-
- return function (prop) {
- if(prop._emitting) return // property is already emitting
- if(prop.timeout == null) { // timeout is null so emit synchonously
- prop._emitting = true
- prop.emit('set', prop.value, prop.old_value)
- delete prop._emitting
- return
- }
-
- list.push(prop)
- prop._emitting = true
- timer = timer || setTimeout(run, prop.timeout) // emit after timeout
- }
-})();
-
-
-/*
* Calculate dependencies
*/
function dependencies(func) {
@@ -247,55 +218,99 @@ o_O.bindFunction = function(fn, callback) {
}
}
+
+// shim layer with setTimeout fallback
+var requestAnimFrame = (function(){
+ return window.requestAnimationFrame ||
+ window.webkitRequestAnimationFrame ||
+ window.mozRequestAnimationFrame ||
+ window.oRequestAnimationFrame ||
+ window.msRequestAnimationFrame ||
+ function( callback ){
+ window.setTimeout(callback, 1000 / 60);
+ };
+})();
+
+
+var queueBinding = (function() {
+ var fns = [], timeout;
+ function run() {
+ while(fns.length)
+ fns.shift()()
+ fns = []
+ timeout = null
+ }
+
+ return function(fn) {
+ fns.push(fn)
+ timeout = timeout || requestAnimFrame(run)
+ }
+})();
+
/*
* el: dom element
* binding: name of the binding rule
* expr: text containing binding specification
* context: the object that we're binding
*/
-o_O.bindElementToRule = function(el, binding, expr, context) {
- if(binding == '"class"') binding = "class"
+
+o_O.bindElementToRule = function(el, rule, expr, context) {
+ rule == '"class"' && (rule = "class");
- var expression = o_O.expression(expr)
+ var expression = o_O.expression(expr);
var trigger = function() {
- return expression.call(context, o_O.helpers)
+ return expression.call(context, o_O.helpers);
}
-
- o_O.bindFunction(trigger, function(x) {
- var $el = $(el),
- y = typeof x == 'function' && x.constructor != o_O
- ? function() { return x.apply(context, arguments) }
- : x
-
- if($.prototype[binding]) {
- if(y instanceof String) y = y.toString() // strange problem
- return $el[binding].call($el, y)
- }
+
+ var emitting;
+ var arg;
+ function run(newarg) {
+ arg = newarg;
+ if(emitting) return;
- var fn = o_O.bindings[binding]
- fn
- ? fn.call(context, y, $el)
- : $el.attr(binding, y)
- })
+ emitting = true;
+
+ function emit() {
+ emitting = false;
+
+ var $el = $(el),
+ y = typeof arg == 'function' && arg.constructor != o_O
+ ? function() { return arg.apply(context, arguments) }
+ : arg;
+ if(y instanceof String) y = y.toString(); // strange problem
+
+ if($.prototype[rule])
+ return $el[rule](y); // return is so we can return false to stop propagation
+
+ var fn = o_O.bindings[rule];
+ fn
+ ? fn.call(context, y, $el)
+ : $el.attr(rule, y);
+ }
+
+ typeof arg == 'function'
+ ? emit()
+ : queueBinding(emit);
+ }
+
+ o_O.bindFunction(trigger, run)
}
+
/*
* Helper function to extract rules from a css like string
*/
function extractRules(str) {
if(!str) return []
+ var rules = trim(str).split(";"), ret = [], i, bits, binding, param, rule
- var rules = trim(str).split(";")
- var ret = []
for(var i=0; i <rules.length; i++) {
- var rule = rules[i]
- rule = trim(rule)
+ rule = trim(rules[i])
if(!rule) continue // for trailing ;
- var bits = map(trim(rule).split(":"), trim)
- var binding = trim(bits.shift())
- var param = trim(bits.join(":"))
-
+ bits = map(trim(rule).split(":"), trim)
+ binding = trim(bits.shift())
+ param = trim(bits.join(":"))
ret.push([binding, param])
}
return ret
@@ -343,9 +358,6 @@ function getTemplate($el) {
return template
}
-
-
-
o_O.helpers = {
// converts a DOM event from an element with a value into its value
// useful for setting properties based on form events
@@ -364,7 +376,6 @@ o_O.helpers = {
}
}
-
/*
_ __|_ _ ._ _ |_ o._ _|o._ _ _
(_|_|_> |_(_)| | | |_)|| |(_||| |(_|_>
@@ -443,7 +454,7 @@ o_O.bindings = {
list.forEach(function(item, index) {
renderItem.call(list, item, $el, index)
})
- list.bind && list.bind($el)
+ list.onbind && list.onbind($el)
},
log: function(context, $el) {
console.log('o_O', context, $el, this)
@@ -495,7 +506,7 @@ extend(model, {
model.emit('set:' + name, model, val, old)
})
- model[name].on('setsync', function(val, old) {
+ model[name].on('set', function(val, old) {
if(val === old) return
var x = {}, y = {}
x[name] = val
@@ -528,6 +539,10 @@ extend(model.prototype, Events, {
toString: function() {
return '#<'+(this.type ? this.type() : 'model')+'>'
},
+ bind: function(el) {
+ o_O.bind(this, el);
+ return this;
+ },
initialize: function(o) {},
valid: function() {
return true
@@ -590,7 +605,7 @@ function array(items) {
this.items = []
this.count = o_O(0)
this.length = 0
- this.count.on('setsync', function(count) {
+ this.count.change(function(count) {
self.length = count
})
if(items) {
@@ -637,6 +652,10 @@ extend(array.prototype, Events, {
}
this.emit.apply(this, arguments)
},
+ bind: function(el) {
+ o_O.bind(this, el)
+ return this
+ },
indexOf: function(o){
return this.items.indexOf(o)
},
@@ -705,7 +724,7 @@ extend(array.prototype, Events, {
: $el.append($$)
o_O.bind(item, $$)
},
- bind: function($el) {
+ onbind: function($el) {
var self = this
this.on('add', function(item, arr, index) {
self.renderItem(item, $el, index)
@@ -802,8 +821,15 @@ o_O.bindingAttribute = 'data-bind';
o_O.inherits = inherits
o_O.extend = extend
o_O.Events = Events
-o_O.VERSION = "0.2.1"
+o_O.VERSION = VERSION
-typeof module != 'undefined' ? module.exports = o_O : window.o_O = o_O
+if(typeof module == 'undefined') {
+ var scripts = document.getElementsByTagName('script')
+ var namespace = scripts[scripts.length-1].src.split('?')[1]
+ window[namespace || 'o_O'] = o_O
+}
+else {
+ module.exports = o_O
+}
}();
Oops, something went wrong.

0 comments on commit 6e0ec76

Please sign in to comment.