stapes
Advanced tools
Comparing version 0.5.1 to 0.6.0
@@ -82,3 +82,3 @@ 'use strict'; | ||
if ( this.model.getAllAsArray().length > 0 ) { | ||
if ( this.model.size() > 0 ) { | ||
this.view.show(); | ||
@@ -85,0 +85,0 @@ } else { |
@@ -79,3 +79,3 @@ 'use strict'; | ||
if ( this.model.getAllAsArray().length > 0 ) { | ||
if ( this.model.size() > 0 ) { | ||
this.view.show(); | ||
@@ -82,0 +82,0 @@ } else { |
'use strict'; | ||
(function() { | ||
var todoView = Stapes.create(), | ||
todoTmpl, | ||
ENTER_KEY_KEYCODE = 13; | ||
var todoView = Stapes.create(); | ||
var todoTmpl; | ||
var ENTER_KEY_KEYCODE = 13; | ||
@@ -7,0 +7,0 @@ function bindEventHandlers() { |
@@ -7,3 +7,3 @@ { | ||
"keywords": ["mvc", "framework", "lightweight"], | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"repository" : { | ||
@@ -10,0 +10,0 @@ "type" : "git", |
367
stapes.js
@@ -20,3 +20,3 @@ // | ||
var VERSION = "0.5.1"; | ||
var VERSION = "0.6"; | ||
@@ -26,157 +26,5 @@ // Global counter for all events in all modules (including mixed in objects) | ||
/** Utility functions | ||
* | ||
* These are more or less modelled on the ones used in Underscore.js, | ||
* but might not be as extensive or failproof. | ||
* However, they are pretty damn useful and can be accessed by using | ||
* the Stapes.util global | ||
*/ | ||
var util = { | ||
"bind" : function(fn, ctx) { | ||
if (util.isObject(fn)) { | ||
// Bind all functions in this object to this object | ||
util.each(fn, function(fun, name) { | ||
if (util.typeOf(fun) === "function") { | ||
fn[name] = util.bind(fun, fn); | ||
} | ||
}); | ||
// Makes _.create() faster | ||
var cachedFunction = function(){}; | ||
return fn; | ||
} else { | ||
if (Function.prototype.bind) { | ||
// Native | ||
return fn.bind(ctx); | ||
} else { | ||
// Non-native | ||
return function() { | ||
return fn.apply(ctx, arguments); | ||
}; | ||
} | ||
} | ||
}, | ||
"clone" : function(obj) { | ||
if (util.isArray(obj)) { | ||
return obj.slice(); | ||
} else if (util.isObject(obj)) { | ||
var newObj = {}; | ||
util.each(obj, function(value, key) { | ||
newObj[key] = value; | ||
}); | ||
return newObj; | ||
} else { | ||
return obj; | ||
} | ||
}, | ||
"create" : function(context) { | ||
var instance; | ||
if (typeof Object.create === "function") { | ||
// Native | ||
instance = Object.create(context); | ||
} else { | ||
// Non-native | ||
var F = function(){}; | ||
F.prototype = context; | ||
instance = new F(); | ||
} | ||
return instance; | ||
}, | ||
"each" : function(list, fn, context) { | ||
if (util.isArray(list)) { | ||
if (Array.prototype.forEach) { | ||
// Native forEach | ||
list.forEach( fn, context ); | ||
} else { | ||
for (var i = 0, l = list.length; i < l; i++) { | ||
fn.call(context, list[i], i); | ||
} | ||
} | ||
} else { | ||
for (var key in list) { | ||
fn.call(context, list[key], key); | ||
} | ||
} | ||
}, | ||
"filter" : function(list, fn, context) { | ||
var results = []; | ||
if (util.isArray(list) && Array.prototype.filter) { | ||
return list.filter(fn, context); | ||
} | ||
util.each(list, function(value) { | ||
if (fn.call(context, value)) { | ||
results.push(value); | ||
} | ||
}); | ||
return results; | ||
}, | ||
"isArray" : function(val) { | ||
return util.typeOf(val) === "array"; | ||
}, | ||
"isObject" : function(val) { | ||
return util.typeOf(val) === "object"; | ||
}, | ||
"keys" : function(list) { | ||
return util.map(list, function(value, key) { | ||
return key; | ||
}); | ||
}, | ||
// from http://stackoverflow.com/a/2117523/152809 | ||
"makeUuid" : function() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | ||
return v.toString(16); | ||
}); | ||
}, | ||
"map" : function(list, fn, context) { | ||
var results = []; | ||
if (util.isArray(list) && Array.prototype.map) { | ||
return list.map(fn, context); | ||
} | ||
util.each(list, function(value, index) { | ||
results.push( fn.call(context, value, index) ); | ||
}); | ||
return results; | ||
}, | ||
"size" : function(list) { | ||
return (util.isArray(list)) ? list.length : util.keys(list).length; | ||
}, | ||
"toArray" : function(val) { | ||
if (util.isObject(val)) { | ||
return util.values(val); | ||
} else { | ||
return Array.prototype.slice.call(val, 0); | ||
} | ||
}, | ||
"typeOf" : function(val) { | ||
return Object.prototype.toString.call(val).replace(/\[object |\]/g, '').toLowerCase(); | ||
}, | ||
"values" : function(list) { | ||
return util.map(list, function(value, key) { | ||
return value; | ||
}); | ||
} | ||
}; | ||
// Private attributes and helper functions, stored in an object so they | ||
@@ -223,8 +71,10 @@ // are overwritable by plugins | ||
util.each(eventMap, function(handler, eventString) { | ||
for (var eventString in eventMap) { | ||
var handler = eventMap[eventString]; | ||
var events = eventString.split(" "); | ||
util.each(events, function(eventType) { | ||
for (var i = 0, l = events.length; i < l; i++) { | ||
var eventType = events[i]; | ||
_.addEvent.call(this, { | ||
"guid" : this._guid, | ||
"guid" : this._guid || this._.guid, | ||
"handler" : handler, | ||
@@ -234,4 +84,4 @@ "scope" : scope, | ||
}); | ||
}, this); | ||
}, this); | ||
} | ||
} | ||
}, | ||
@@ -254,5 +104,14 @@ | ||
clone : function(obj) { | ||
return _.extend({}, obj); | ||
}, | ||
create : function(obj) { | ||
cachedFunction.prototype = obj; | ||
return new cachedFunction(); | ||
}, | ||
// Stapes objects have some extra properties that are set on creation | ||
createModule : function( context ) { | ||
var instance = util.create( context ); | ||
var instance = _.create( context ); | ||
@@ -271,12 +130,51 @@ _.addGuid( instance, true ); | ||
util.each(_.eventHandlers[explicitGuid][type], function(event) { | ||
var handlers = _.eventHandlers[explicitGuid][type]; | ||
for (var i = 0, l = handlers.length; i < l; i++) { | ||
// Clone the event to prevent issue #19 | ||
var event = _.extend({}, handlers[i]); | ||
var scope = (event.scope) ? event.scope : this; | ||
if (explicitType) { | ||
event.type = explicitType; | ||
} | ||
event.scope = scope; | ||
event.handler.call(event.scope, data, event); | ||
}, this); | ||
} | ||
}, | ||
extend : function(obj, props) { | ||
for (var key in props) { | ||
obj[key] = props[key]; | ||
} | ||
return obj; | ||
}, | ||
// from http://stackoverflow.com/a/2117523/152809 | ||
"makeUuid" : function() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { | ||
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); | ||
return v.toString(16); | ||
}); | ||
}, | ||
removeAttribute : function(key, silent) { | ||
silent = silent || false; | ||
// Actually delete the item | ||
delete _.attr(this._guid)[key]; | ||
// If 'silent' is set, do not throw any events | ||
if (silent) { | ||
return this; | ||
} | ||
this.emit('change', key); | ||
this.emit('change:' + key); | ||
this.emit('remove', key); | ||
this.emit('remove:' + key); | ||
}, | ||
removeEventHandler : function(type, handler) { | ||
@@ -287,7 +185,12 @@ var handlers = _.eventHandlers[this._guid]; | ||
// Remove a specific handler | ||
util.each(handlers[type], function(eventObject, index) { | ||
if (eventObject.handler === handler) { | ||
handlers[type].splice(index--, 1); | ||
handlers = handlers[type]; | ||
if (!handlers) return; | ||
for (var i = 0, l = handlers.length, h; i < l; i++) { | ||
h = handlers[i].handler; | ||
if (h && h === handler) { | ||
handlers.splice(i--, 1); | ||
l--; | ||
} | ||
}, this); | ||
} | ||
} else if (type) { | ||
@@ -302,6 +205,8 @@ // Remove all handlers for a specific type | ||
setAttribute : function(key, value) { | ||
setAttribute : function(key, value, silent) { | ||
silent = silent || false; | ||
// We need to do this before we actually add the item :) | ||
var itemExists = this.has(key), | ||
oldValue = _.attr(this._guid)[key]; | ||
var itemExists = this.has(key); | ||
var oldValue = _.attr(this._guid)[key]; | ||
@@ -316,2 +221,7 @@ // Is the value different than the oldValue? If not, ignore this call | ||
// If 'silent' flag is set, do not throw any events | ||
if (silent) { | ||
return; | ||
} | ||
// Throw a generic event | ||
@@ -344,5 +254,13 @@ this.emit('change', key); | ||
"typeOf" : function(val) { | ||
if (val === null || typeof val === "undefined") { | ||
return String(val); | ||
} else { | ||
return Object.prototype.toString.call(val).replace(/\[object |\]/g, '').toLowerCase(); | ||
} | ||
}, | ||
updateAttribute : function(key, fn) { | ||
var item = this.get(key), | ||
newValue = fn( util.clone(item) ); | ||
newValue = fn( _.clone(item) ); | ||
@@ -358,3 +276,7 @@ _.setAttribute.call(this, key, newValue); | ||
util.each(types.split(" "), function(type) { | ||
var splittedTypes = types.split(" "); | ||
for (var i = 0, l = splittedTypes.length; i < l; i++) { | ||
var type = splittedTypes[i]; | ||
// First 'all' type events: is there an 'all' handler in the | ||
@@ -382,3 +304,3 @@ // global stack? | ||
} | ||
}, this); | ||
} | ||
}, | ||
@@ -401,12 +323,14 @@ | ||
each : function(fn, ctx) { | ||
util.each(_.attr(this._guid), fn, ctx || this); | ||
var attr = _.attr(this._guid); | ||
for (var key in attr) { | ||
var value = attr[key]; | ||
fn.call(ctx || this, value, key); | ||
} | ||
}, | ||
extend : function(objectOrValues, valuesIfObject) { | ||
var object = (valuesIfObject) ? objectOrValues : this, | ||
values = (valuesIfObject) ? valuesIfObject : objectOrValues; | ||
var object = (valuesIfObject) ? objectOrValues : this; | ||
var values = (valuesIfObject) ? valuesIfObject : objectOrValues; | ||
util.each(values, function(value, key) { | ||
object[key] = value; | ||
}); | ||
_.extend(object, values); | ||
@@ -417,3 +341,12 @@ return this; | ||
filter : function(fn) { | ||
return util.filter(_.attr(this._guid), fn); | ||
var filtered = []; | ||
var attributes = _.attr(this._guid); | ||
for (var key in attributes) { | ||
if ( fn.call(this, attributes[key], key)) { | ||
filtered.push( attributes[key] ); | ||
} | ||
} | ||
return filtered; | ||
}, | ||
@@ -431,15 +364,20 @@ | ||
getAll : function() { | ||
return util.clone( _.attr(this._guid) ); | ||
return _.clone( _.attr(this._guid) ); | ||
}, | ||
getAllAsArray : function() { | ||
var arr = util.map(_.attr(this._guid), function(value, key) { | ||
if (util.isObject(value)) { | ||
var arr = []; | ||
var attributes = _.attr(this._guid); | ||
for (var key in attributes) { | ||
var value = attributes[key]; | ||
if (_.typeOf(value) === "object") { | ||
value.id = key; | ||
} | ||
return value; | ||
}); | ||
arr.push(value); | ||
} | ||
return util.clone( arr ); | ||
return arr; | ||
}, | ||
@@ -452,40 +390,43 @@ | ||
// Akin to set(), but makes a unique id | ||
push : function(input) { | ||
if (util.isArray(input)) { | ||
util.each(input, function(value) { | ||
_.setAttribute.call(this, util.makeUuid(), value); | ||
}, this); | ||
push : function(input, silent) { | ||
if (_.typeOf(input) === "array") { | ||
for (var i = 0, l = input.length; i < l; i++) { | ||
_.setAttribute.call(this, _.makeUuid(), input[i]); | ||
} | ||
} else { | ||
_.setAttribute.call(this, util.makeUuid(), input); | ||
_.setAttribute.call(this, _.makeUuid(), input, silent || false); | ||
} | ||
return this; | ||
}, | ||
remove : function(input) { | ||
remove : function(input, silent) { | ||
if (typeof input === "function") { | ||
this.each(function(item, key) { | ||
if (input(item)) { | ||
delete _.attr(this._guid)[key]; | ||
this.emit('remove change'); | ||
_.removeAttribute.call(this, key, silent); | ||
} | ||
}); | ||
} else { | ||
if (this.has(input)) { | ||
delete _.attr(this._guid)[input]; | ||
this.emit('remove change'); | ||
} | ||
// nb: checking for exists happens in removeAttribute | ||
_.removeAttribute.call(this, input, silent || false); | ||
} | ||
return this; | ||
}, | ||
set : function(objOrKey, value) { | ||
if (util.isObject(objOrKey)) { | ||
util.each(objOrKey, function(value, key) { | ||
_.setAttribute.call(this, key, value); | ||
}, this); | ||
set : function(objOrKey, value, silent) { | ||
if (typeof objOrKey === "object") { | ||
for (var key in objOrKey) { | ||
_.setAttribute.call(this, key, objOrKey[key]); | ||
} | ||
} else { | ||
_.setAttribute.call(this, objOrKey, value); | ||
_.setAttribute.call(this, objOrKey, value, silent || false); | ||
} | ||
return this; | ||
}, | ||
size : function() { | ||
return util.size( _.attributes[this._guid] ); | ||
return Object.keys(_.attributes[this._guid]).length; | ||
}, | ||
@@ -501,2 +442,4 @@ | ||
} | ||
return this; | ||
} | ||
@@ -513,5 +456,3 @@ }; | ||
"extend" : function(obj) { | ||
util.each(obj, function(value, key) { | ||
_.Module[key] = value; | ||
}); | ||
return _.extend(_.Module, obj); | ||
}, | ||
@@ -524,7 +465,3 @@ | ||
util.each(Events, function(value, key) { | ||
obj[key] = value; | ||
}); | ||
return obj; | ||
return _.extend(obj, Events); | ||
}, | ||
@@ -536,4 +473,2 @@ | ||
"util" : util, | ||
"version" : VERSION | ||
@@ -540,0 +475,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
/*! Stapes.js v0.5.1 - (c) Hay Kranen, released under the MIT license - http://hay.github.com/stapes */ | ||
(function(){"use strict";var a="0.5.1",b=1,c={bind:function(a,b){if(c.isObject(a)){c.each(a,function(b,d){c.typeOf(b)==="function"&&(a[d]=c.bind(b,a))});return a}return Function.prototype.bind?a.bind(b):function(){return a.apply(b,arguments)}},clone:function(a){if(c.isArray(a))return a.slice();if(c.isObject(a)){var b={};c.each(a,function(a,c){b[c]=a});return b}return a},create:function(a){var b;if(typeof Object.create=="function")b=Object.create(a);else{var c=function(){};c.prototype=a,b=new c}return b},each:function(a,b,d){if(c.isArray(a))if(Array.prototype.forEach)a.forEach(b,d);else for(var e=0,f=a.length;e<f;e++)b.call(d,a[e],e);else for(var g in a)b.call(d,a[g],g)},filter:function(a,b,d){var e=[];if(c.isArray(a)&&Array.prototype.filter)return a.filter(b,d);c.each(a,function(a){b.call(d,a)&&e.push(a)});return e},isArray:function(a){return c.typeOf(a)==="array"},isObject:function(a){return c.typeOf(a)==="object"},keys:function(a){return c.map(a,function(a,b){return b})},makeUuid:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=Math.random()*16|0,c=a=="x"?b:b&3|8;return c.toString(16)})},map:function(a,b,d){var e=[];if(c.isArray(a)&&Array.prototype.map)return a.map(b,d);c.each(a,function(a,c){e.push(b.call(d,a,c))});return e},size:function(a){return c.isArray(a)?a.length:c.keys(a).length},toArray:function(a){return c.isObject(a)?c.values(a):Array.prototype.slice.call(a,0)},typeOf:function(a){return Object.prototype.toString.call(a).replace(/\[object |\]/g,"").toLowerCase()},values:function(a){return c.map(a,function(a,b){return a})}},d={attributes:{},eventHandlers:{"-1":{}},guid:-1,addEvent:function(a){d.eventHandlers[a.guid][a.type]||(d.eventHandlers[a.guid][a.type]=[]),d.eventHandlers[a.guid][a.type].push({guid:a.guid,handler:a.handler,scope:a.scope,type:a.type})},addEventHandler:function(a,b,e){var f={},g;typeof a=="string"?(g=e||!1,f[a]=b):(g=b||!1,f=a),c.each(f,function(a,b){var e=b.split(" ");c.each(e,function(b){d.addEvent.call(this,{guid:this._guid,handler:a,scope:g,type:b})},this)},this)},addGuid:function(a,c){if(!a._guid||!!c)a._guid=b++,d.attributes[a._guid]={},d.eventHandlers[a._guid]={}},attr:function(a){return d.attributes[a]},createModule:function(a){var b=c.create(a);d.addGuid(b,!0),f.mixinEvents(b);return b},emitEvents:function(a,b,e,f){e=e||!1,f=f||this._guid,c.each(d.eventHandlers[f][a],function(a){var c=a.scope?a.scope:this;e&&(a.type=e),a.scope=c,a.handler.call(a.scope,b,a)},this)},removeEventHandler:function(a,b){var e=d.eventHandlers[this._guid];a&&b?c.each(e[a],function(c,d){c.handler===b&&e[a].splice(d--,1)},this):a?delete e[a]:d.eventHandlers[this._guid]={}},setAttribute:function(a,b){var c=this.has(a),e=d.attr(this._guid)[a];if(b!==e){d.attr(this._guid)[a]=b,this.emit("change",a),this.emit("change:"+a,b);var f={key:a,newValue:b,oldValue:e||null};this.emit("mutate",f),this.emit("mutate:"+a,f);var g=c?"update":"create";this.emit(g,a),this.emit(g+":"+a,b)}},updateAttribute:function(a,b){var e=this.get(a),f=b(c.clone(e));d.setAttribute.call(this,a,f)}},e={emit:function(a,b){b=typeof b=="undefined"?null:b,c.each(a.split(" "),function(a){d.eventHandlers[-1].all&&d.emitEvents.call(this,"all",b,a,-1),d.eventHandlers[-1][a]&&d.emitEvents.call(this,a,b,a,-1),typeof this._guid=="number"&&(d.eventHandlers[this._guid].all&&d.emitEvents.call(this,"all",b,a),d.eventHandlers[this._guid][a]&&d.emitEvents.call(this,a,b))},this)},off:function(){d.removeEventHandler.apply(this,arguments)},on:function(){d.addEventHandler.apply(this,arguments)}};d.Module={create:function(){return d.createModule(this)},each:function(a,b){c.each(d.attr(this._guid),a,b||this)},extend:function(a,b){var d=b?a:this,e=b?b:a;c.each(e,function(a,b){d[b]=a});return this},filter:function(a){return c.filter(d.attr(this._guid),a)},get:function(a){if(typeof a=="string")return this.has(a)?d.attr(this._guid)[a]:null;if(typeof a=="function"){var b=this.filter(a);return b.length?b[0]:null}},getAll:function(){return c.clone(d.attr(this._guid))},getAllAsArray:function(){var a=c.map(d.attr(this._guid),function(a,b){c.isObject(a)&&(a.id=b);return a});return c.clone(a)},has:function(a){return typeof d.attr(this._guid)[a]!="undefined"},push:function(a){c.isArray(a)?c.each(a,function(a){d.setAttribute.call(this,c.makeUuid(),a)},this):d.setAttribute.call(this,c.makeUuid(),a)},remove:function(a){typeof a=="function"?this.each(function(b,c){a(b)&&(delete d.attr(this._guid)[c],this.emit("remove change"))}):this.has(a)&&(delete d.attr(this._guid)[a],this.emit("remove change"))},set:function(a,b){c.isObject(a)?c.each(a,function(a,b){d.setAttribute.call(this,b,a)},this):d.setAttribute.call(this,a,b)},size:function(){return c.size(d.attributes[this._guid])},update:function(a,b){typeof a=="string"?d.updateAttribute.call(this,a,b):typeof a=="function"&&this.each(function(b,c){d.updateAttribute.call(this,c,a)})}};var f={_:d,create:function(){return d.createModule(d.Module)},extend:function(a){c.each(a,function(a,b){d.Module[b]=a})},mixinEvents:function(a){a=a||{},d.addGuid(a),c.each(e,function(b,c){a[c]=b});return a},on:function(){d.addEventHandler.apply(this,arguments)},util:c,version:a};typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(exports=module.exports=f),exports.Stapes=f):typeof define=="function"&&define.amd?define(function(){return f}):window.Stapes=f})() | ||
/*! Stapes.js v0.6 < http://hay.github.com/stapes > */ | ||
(function(){"use strict";var a="0.6",b=1,c=function(){},d={attributes:{},eventHandlers:{"-1":{}},guid:-1,addEvent:function(a){d.eventHandlers[a.guid][a.type]||(d.eventHandlers[a.guid][a.type]=[]),d.eventHandlers[a.guid][a.type].push({guid:a.guid,handler:a.handler,scope:a.scope,type:a.type})},addEventHandler:function(a,b,c){var e={},f;typeof a=="string"?(f=c||!1,e[a]=b):(f=b||!1,e=a);for(var g in e){var h=e[g],i=g.split(" ");for(var j=0,k=i.length;j<k;j++){var l=i[j];d.addEvent.call(this,{guid:this._guid||this._.guid,handler:h,scope:f,type:l})}}},addGuid:function(a,c){if(!a._guid||!!c)a._guid=b++,d.attributes[a._guid]={},d.eventHandlers[a._guid]={}},attr:function(a){return d.attributes[a]},clone:function(a){return d.extend({},a)},create:function(a){c.prototype=a;return new c},createModule:function(a){var b=d.create(a);d.addGuid(b,!0),f.mixinEvents(b);return b},emitEvents:function(a,b,c,e){c=c||!1,e=e||this._guid;var f=d.eventHandlers[e][a];for(var g=0,h=f.length;g<h;g++){var i=d.extend({},f[g]),j=i.scope?i.scope:this;c&&(i.type=c),i.scope=j,i.handler.call(i.scope,b,i)}},extend:function(a,b){for(var c in b)a[c]=b[c];return a},makeUuid:function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=Math.random()*16|0,c=a=="x"?b:b&3|8;return c.toString(16)})},removeAttribute:function(a,b){b=b||!1,delete d.attr(this._guid)[a];if(b)return this;this.emit("change",a),this.emit("change:"+a),this.emit("remove",a),this.emit("remove:"+a)},removeEventHandler:function(a,b){var c=d.eventHandlers[this._guid];if(a&&b){c=c[a];if(!c)return;for(var e=0,f=c.length,g;e<f;e++)g=c[e].handler,g&&g===b&&(c.splice(e--,1),f--)}else a?delete c[a]:d.eventHandlers[this._guid]={}},setAttribute:function(a,b,c){c=c||!1;var e=this.has(a),f=d.attr(this._guid)[a];if(b!==f){d.attr(this._guid)[a]=b;if(c)return;this.emit("change",a),this.emit("change:"+a,b);var g={key:a,newValue:b,oldValue:f||null};this.emit("mutate",g),this.emit("mutate:"+a,g);var h=e?"update":"create";this.emit(h,a),this.emit(h+":"+a,b)}},typeOf:function(a){return Object.prototype.toString.call(a).replace(/\[object |\]/g,"").toLowerCase()},updateAttribute:function(a,b){var c=this.get(a),e=b(d.clone(c));d.setAttribute.call(this,a,e)}},e={emit:function(a,b){b=typeof b=="undefined"?null:b;var c=a.split(" ");for(var e=0,f=c.length;e<f;e++){var g=c[e];d.eventHandlers[-1].all&&d.emitEvents.call(this,"all",b,g,-1),d.eventHandlers[-1][g]&&d.emitEvents.call(this,g,b,g,-1),typeof this._guid=="number"&&(d.eventHandlers[this._guid].all&&d.emitEvents.call(this,"all",b,g),d.eventHandlers[this._guid][g]&&d.emitEvents.call(this,g,b))}},off:function(){d.removeEventHandler.apply(this,arguments)},on:function(){d.addEventHandler.apply(this,arguments)}};d.Module={create:function(){return d.createModule(this)},each:function(a,b){var c=d.attr(this._guid);for(var e in c){var f=c[e];a.call(b||this,f,e)}},extend:function(a,b){var c=b?a:this,e=b?b:a;d.extend(c,e);return this},filter:function(a){var b=[],c=d.attr(this._guid);for(var e in c)a.call(this,c[e],e)&&b.push(c[e]);return b},get:function(a){if(typeof a=="string")return this.has(a)?d.attr(this._guid)[a]:null;if(typeof a=="function"){var b=this.filter(a);return b.length?b[0]:null}},getAll:function(){return d.clone(d.attr(this._guid))},getAllAsArray:function(){var a=[],b=d.attr(this._guid);for(var c in b){var e=b[c];d.typeOf(e)==="object"&&(e.id=c),a.push(e)}return a},has:function(a){return typeof d.attr(this._guid)[a]!="undefined"},push:function(a,b){if(d.typeOf(a)==="array")for(var c=0,e=a.length;c<e;c++)d.setAttribute.call(this,d.makeUuid(),a[c]);else d.setAttribute.call(this,d.makeUuid(),a,b||!1);return this},remove:function(a,b){typeof a=="function"?this.each(function(c,e){a(c)&&d.removeAttribute.call(this,e,b)}):d.removeAttribute.call(this,a,b||!1);return this},set:function(a,b,c){if(typeof a=="object")for(var e in a)d.setAttribute.call(this,e,a[e]);else d.setAttribute.call(this,a,b,c||!1);return this},size:function(){return Object.keys(d.attributes[this._guid]).length},update:function(a,b){typeof a=="string"?d.updateAttribute.call(this,a,b):typeof a=="function"&&this.each(function(b,c){d.updateAttribute.call(this,c,a)});return this}};var f={_:d,create:function(){return d.createModule(d.Module)},extend:function(a){return d.extend(d.Module,a)},mixinEvents:function(a){a=a||{},d.addGuid(a);return d.extend(a,e)},on:function(){d.addEventHandler.apply(this,arguments)},version:a};typeof exports!="undefined"?(typeof module!="undefined"&&module.exports&&(exports=module.exports=f),exports.Stapes=f):typeof define=="function"&&define.amd?define(function(){return f}):window.Stapes=f})() |
129
test/test.js
var undef; | ||
var util = Stapes.util; | ||
// Some shims for IE | ||
if (!Object.keys) { | ||
Object.keys = function(obj) { | ||
var arr = []; | ||
for (var key in obj) { | ||
arr.push(key); | ||
} | ||
return arr; | ||
} | ||
} | ||
module("set"); | ||
@@ -16,4 +27,11 @@ | ||
ok(key === 'name' || key === 'instrument', 'change event when name is set'); | ||
if (key === "silent") { | ||
ok(false, "Silent event should not trigger"); | ||
} | ||
}, | ||
'change:silent' : function(key) { | ||
ok(false, "Silent event should not trigger"); | ||
}, | ||
'change:name' : function(value) { | ||
@@ -54,2 +72,3 @@ equal(value, 'Emmylou', 'name attribute changed'); | ||
module.set('instrument', 'guitar'); // Change event should only be thrown once! | ||
module.set('silent', 'silent', true); // silent events should not trigger anything | ||
}); | ||
@@ -84,2 +103,35 @@ | ||
module("remove"); | ||
test("remove", function() { | ||
var module = Stapes.create(); | ||
module.set('foo', 'bar'); | ||
module.set('silent', 'silent'); | ||
module.on({ | ||
'change': function( key ){ | ||
ok(key === 'foo', 'change event with key of attribute'); | ||
}, | ||
'change:foo': function(key, e){ | ||
ok(e.type === 'change:foo', 'change:key event'); | ||
}, | ||
'remove': function( key ){ | ||
ok(key === 'foo', 'remove event with key of attribute'); | ||
}, | ||
'remove:foo': function(key, e){ | ||
ok(e.type === 'remove:foo', 'change:key event'); | ||
}, | ||
'remove:silent' : function() { | ||
ok(false, 'silent event should not trigger'); | ||
} | ||
}); | ||
module.remove('foo'); | ||
module.remove('silent', true); // should not trigger because of silent flag | ||
}) | ||
module("iterators"); | ||
@@ -159,13 +211,11 @@ | ||
module("util"); | ||
test("util.typeof", function() { | ||
ok(Stapes.util.typeOf( {} ) === "object", "typeof {} = object"); | ||
ok(Stapes.util.typeOf( [] ) === "array", "typeof [] = array"); | ||
ok(Stapes.util.typeOf( function(){} ) === "function", "typeof function(){} = function"); | ||
ok(Stapes.util.typeOf( true ) === "boolean", "typeof true = boolean"); | ||
ok(Stapes.util.typeOf( 1 ) === "number", "typeof 1 = number"); | ||
ok(Stapes.util.typeOf( '' ) === "string", "typeof '' = string"); | ||
ok(Stapes.util.typeOf( null ) === "null", "typeof null = null"); | ||
ok(Stapes.util.typeOf( undefined ) === "undefined", "typeof undefined = undefined"); | ||
test("_.typeof", function() { | ||
ok(Stapes._.typeOf( {} ) === "object", "typeof {} = object"); | ||
ok(Stapes._.typeOf( [] ) === "array", "typeof [] = array"); | ||
ok(Stapes._.typeOf( function(){} ) === "function", "typeof function(){} = function"); | ||
ok(Stapes._.typeOf( true ) === "boolean", "typeof true = boolean"); | ||
ok(Stapes._.typeOf( 1 ) === "number", "typeof 1 = number"); | ||
ok(Stapes._.typeOf( '' ) === "string", "typeof '' = string"); | ||
ok(Stapes._.typeOf( null ) === "null", "typeof null = null"); | ||
ok(Stapes._.typeOf( undefined ) === "undefined", "typeof undefined = undefined"); | ||
}); | ||
@@ -187,3 +237,3 @@ | ||
ok(util.size(events) === 2, "Event handlers are set"); | ||
ok(Object.keys(events).length === 2, "Event handlers are set"); | ||
@@ -200,3 +250,3 @@ module.off("foo", handler); | ||
ok(util.size(Stapes._.eventHandlers[module._guid]) === 0, "no handlers for module"); | ||
ok(Object.keys(Stapes._.eventHandlers[module._guid]).length === 0, "no handlers for module"); | ||
}); | ||
@@ -218,2 +268,8 @@ | ||
ok(f._guid !== g._guid, "_guid of two newly created objects should not be the same"); | ||
Stapes.on('foo', function(data, e) { | ||
ok(e.type === "foo", "Check if local events bubble through the Stapes object"); | ||
}); | ||
g.emit('foo'); | ||
}); | ||
@@ -226,2 +282,47 @@ | ||
ok(module2._guid === (module1._guid + 1), "A new module should increase its guid by 1"); | ||
}); | ||
test("event scope", function() { | ||
var module1 = Stapes.create(); | ||
var module2 = Stapes.create(); | ||
var firstDone = false; | ||
module1.on('eventscope', function(data, e) { | ||
ok(e.scope === module1, "Scope of event should be the emitting model"); | ||
}); | ||
module2.on('eventscope', function(data, e) { | ||
ok(e.scope === module2, "Scope of event should be the emitting model"); | ||
}); | ||
Stapes.on('eventscope', function(data, e) { | ||
if (firstDone) { | ||
ok(e.scope !== module1, "Scope of event from global Stapes object should not be the mixed with other models"); | ||
ok(e.scope === module2, "Scope of event from global Stapes object should be the emitting model"); | ||
} else { | ||
ok(e.scope === module1, "Scope of event from global Stapes object should be the emitting model"); | ||
firstDone = true; | ||
} | ||
}); | ||
Stapes.on('all', function(data, e) { | ||
if (e.type === "eventscope") { | ||
// Prevent other events from other tests getting here | ||
ok(e.scope === module1 || e.scope === module2, "Scope event from on 'all' handler on Stapes.on should be emitting model"); | ||
} | ||
}); | ||
module1.emit('eventscope'); | ||
module2.emit('eventscope'); | ||
}); | ||
test("chaining", function() { | ||
var module = Stapes.create().set('foo', true); | ||
ok(!!module.get && module.get('foo'), "set() should return the object"); | ||
module = module.update('foo', function() { return true; }); | ||
ok(!!module.get && module.get('foo'), "update() should return the object"); | ||
module = module.remove('foo'); | ||
ok(!!module.get && module.get('foo') === null, "remove() should return the object"); | ||
module = module.push(true); | ||
ok(!!module.get && module.size() === 1, "push() should return the object"); | ||
}); |
Sorry, the diff of this file is not supported yet
573283
40
11389