Browse files

Fix requirejs and amd definitions, remove async autoloader to support it

  • Loading branch information...
1 parent 33ef872 commit 396841575e1abeae6441eb0ff5a5cdc64e55a973 @weikinhuang committed Mar 8, 2013
Showing with 89 additions and 107 deletions.
  1. +1 −1 config.js
  2. +37 −34 dist/classify.js
  3. +3 −3 dist/classify.min.js
  4. +2 −6 docs/classify.docs.js
  5. +1 −1 package.json
  6. +10 −8 src/export.js
  7. +24 −23 src/namespace.js
  8. +11 −31 test/namespace.js
View
2 config.js
@@ -1,6 +1,6 @@
module.exports = function(build) {
// set basic info about the repo
- build.setNameVersion("classify", "0.10.1");
+ build.setNameVersion("classify", "0.10.2");
// set the url of this repo
build.setRepoName("https://github.com/weikinhuang/Classify");
View
71 dist/classify.js
@@ -1,11 +1,11 @@
/*!
- * Classify JavaScript Library v0.10.1
+ * Classify JavaScript Library v0.10.2
* http://www.closedinterval.com/
*
* Copyright 2011-2012, Wei Kin Huang
* Classify is freely distributable under the MIT license.
*
- * Date: Wed, 03 Oct 2012 03:06:23 GMT
+ * Date: Fri, 08 Mar 2013 21:52:25 GMT
*/
(function(root, undefined) {
"use strict";
@@ -832,10 +832,10 @@ provide = function(namespace, base) {
return base;
},
// ability to de-reference string into it's classes
-dereference = function(base, arg, ref) {
+dereference = function(ns, arg, ref) {
// Allow parent classes to be passed in as a string for lookup
if (typeof arg === "string") {
- ref = base[arg] || getGlobalNamespace().get(arg) || null;
+ ref = ns.get(arg);
if (!ref) {
throw new Error("Invalid parent class [" + arg + "] specified.");
}
@@ -844,7 +844,7 @@ dereference = function(base, arg, ref) {
// if we have an object, then that's what we want, otherwise arrays
// we need to loop through and convert them to the proper objects
return !isArray(arg) ? arg : map(arg, function(prop) {
- return dereference(base, prop);
+ return dereference(ns, prop);
});
};
@@ -867,20 +867,18 @@ var Namespace = create({
self = this,
// Retrieve the namespaced container
ref = provide(namespace, self),
- // Create a reference to the master classes array
- deref = self.ref,
// fix the arguments & create the class
c = create.apply(null, map(args, function(v) {
- return dereference(deref, v);
+ return dereference(self, v);
}));
// Assign the magic properties of the class's name and namespace
c._name_ = name;
c._namespace_ = fullname;
// fix the issue with the extends function referencing string classes
c.extend = c.prototype.extend = function() {
return create.apply(null, [ c ].concat(map(arguments, function(v) {
- return dereference(deref, v);
+ return dereference(self, v);
})));
};
// give classes the ability to always store the namespace for chaining
@@ -892,7 +890,7 @@ var Namespace = create({
};
// Assign the classes to the namespaced references
ref[name] = c;
- deref[fullname] = c;
+ self.ref[fullname] = c;
// Return the new class
return c;
},
@@ -951,24 +949,27 @@ var Namespace = create({
exists : function(classname) {
return !!this.ref[classname];
},
- get : function(name, callback) {
- // ability to load a class async if a callback is passed in
- if (isFunction(callback)) {
- if (this.ref[name]) {
- callback(this.ref[name]);
- } else {
- this.load(name, callback);
- }
- return this;
+ get : function(name) {
+ var tmp;
+ // already defined, return it
+ if (this.ref[name]) {
+ return this.ref[name];
+ }
+ // use the autoloader if defined
+ tmp = this.load(name);
+ // return if loaded
+ if (tmp !== null) {
+ return tmp;
}
- // otherwise just return the class if it is already avaliable or reach into the global namespace
- return this.ref[name] || (this.name !== global_namespace && getGlobalNamespace().get(name)) || null;
+ // reach into the global namespace
+ if (this.name !== global_namespace) {
+ return getGlobalNamespace().get(name);
+ }
+ // no class found
+ return null;
},
load : function(name, callback) {
- if (callback) {
- callback(this.ref[name] || null);
- }
- return this;
+ return this.ref[name] || null;
},
setAutoloader : function(callback) {
// make sure the callback is a function
@@ -1107,7 +1108,7 @@ Classify = create({
// store clean references to these methods
extend(Classify, exportNames, {
// object version number
- version : "0.10.1",
+ version : "0.10.2",
// direct access functions
create : create,
@@ -1118,19 +1119,21 @@ extend(Classify, exportNames, {
extend : extend
});
-// Export the Classify object for **CommonJS**, with backwards-compatibility for the
-// old "require()" API. If we're not in CommonJS, add "Classify" to the global object.
-if (typeof module !== "undefined" && module.exports) {
- module.exports = Classify;
- // create a circular reference
- Classify.Classify = Classify;
-} else if (typeof root.define === "function" && root.define.amd) {
+/*global define */
+if (typeof define === "function" && define.amd) {
// Export Classify as an AMD module only if there is a AMD module loader,
// use lowercase classify, because AMD modules are usually loaded with filenames
// and Classify would usually be loaded with lowercase classify.js
- root.define("classify", function() {
+ define(function() {
return Classify;
});
+}
+if (typeof module !== "undefined" && module.exports) {
+ // Export the Classify object for **CommonJS**, with backwards-compatibility for the
+ // old "require()" API. If we're not in CommonJS, add "Classify" to the global object.
+ module.exports = Classify;
+ // create a circular reference
+ Classify.Classify = Classify;
} else {
// store previous value of root.Classify
var root_value = root.Classify;
View
6 dist/classify.min.js
@@ -1,9 +1,9 @@
/*!
- * Classify JavaScript Library v0.10.1
+ * Classify JavaScript Library v0.10.2
* http://www.closedinterval.com/
*
* Copyright 2011-2012, Wei Kin Huang
* Classify is freely distributable under the MIT license.
*
- * Date: Wed, 03 Oct 2012 03:06:23 GMT
- */;(function(e,t){"use strict";var n,r,i,s,o,u,a,f,l,c,h,p,d,v="prototype",m={},g=!{toString:null}.propertyIsEnumerable("toString"),y=g?"hasOwnProperty,valueOf,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,constructor".split(","):[],b=y.length,w=Object[v],E=w.toString,S=w.hasOwnProperty,x=/^(?:boolean|number|string|undefined)$/,T=function(e){return e===null||x.test(typeof e)},N=function(e){return E.call(e)==="[object Function]"},C=function(e){return e&&e[v]&&N(e)},k=Array.isArray||function(e){return E.call(e)==="[object Array]"},L=/^\s*function\s+.+?\(.*?\)\s*\{\s*\[native code\]\s*\}\s*$/,A=function(e){return N(e)&&L.test(e+"")},O=function(e){var t=[],n;for(n in e)t[t.length]=n;if(g)for(n=0;n<b;++n)S.call(e,y[n])&&(t[t.length]=y[n]);return t},M=function(e){return k(e)?e:[e]},_=function(e){return Array[v].slice.call(e,0)},D=Array[v].indexOf?function(e,t){return e.indexOf(t)}:function(e,t){var n=0,r=e.length;for(;n<r;++n)if(e[n]===t)return n;return-1},P=function(e,t){return e.__original_=t,e},H=function(e,n,r){if(!e)return e;var i,s=0,o=e.length,u;if(o===t||N(e)){u=O(e),o=u.length;for(i=e[u[0]];s<o;i=e[u[++s]])if(n.call(r||i,i,u[s],e)===!1)break}else for(i=e[0];s<o;i=e[++s])if(n.call(r||i,i,s,e)===!1)break;return e},B=function(e,t){var n=[];return H(e,function(r,i){n[n.length]=t(r,i,e)}),n},j=function(e,t){var n=[];return H(e,function(e){e!==t&&(n[n.length]=e)}),n},F=function(){var e=_(arguments),t=e.shift();return H(e,function(e){H(O(e),function(n){t[n]=e[n]})}),t},I=/^(?:superclass|subclass|implement|observable|bindings|extend|prototype|applicate|(?:add|remove)(?:Static|Observable|Aliased|Bound)Property)$/,q=/^__/,R={},U=[],z=[],W=[],X=[],V=[U,z,W,X],$=["onCreate","onPropAdd","onPropRemove","onInit"],J=A(Object.create)?Object.create:function(e){var t=function(){};return t[v]=e,new t},K=function(e,t,n){e[t]=n},Q=function(){var e=function(){};return e[v].init=function(){},e.superclass=null,e.subclass=[],e.implement=[],e[v].constructor=Q,e[v].self=Q,e.__isclass_=!0,e}(),G=function(e,n){return P(function(){var r=this.parent,i;return this.parent=e,i=n.apply(this,arguments),r===t?delete this.parent:this.parent=r,i},n)},Y=function(e,t){if(R[e])throw Error('Adding duplicate mutator "'+e+'".');R[e]=t,t.name=e,t.propTest=RegExp("^__"+e+"_"),t.propPrefix="__"+e+"_",H($,function(e,n){t[e]&&V[n].push(t)})},Z=function(e){var t=R[e];if(!t)throw Error("Removing unknown mutator.");H($,function(e,n){if(t[e]){var r=D(V[n],t);r>-1&&V[n].splice(r,1)}}),R[e]=null;try{delete R[e]}catch(n){}},et=function(e,t,n,r){var i,s,o;if(r===w[n])return;if(q.test(n)){i=!1,H(z,function(s){if(s.propTest.test(n))return n===s.propPrefix?H(r,function(n,r){s.onPropAdd.call(s,e,t,r,n)}):s.onPropAdd.call(s,e,t,n.replace(s.propTest,""),r),i=!0,!1});if(i)return}s=t[v][n],o=e[v],K(o,n,N(r)&&!r.__isclass_&&N(s)?G(s,r):r),N(r)&&H(e.subclass,function(e){N(e[v][n])&&!e[v][n].__original_&&K(e[v],n,G(o[n],e[v][n]))})},tt=function(e,t){var n=!1;if(q.test(t)){H(W,function(r){if(r.propTest.test(t))return r.onPropRemove.call(r,e,t.replace(r.propTest,"")),n=!0,!1});if(n)return}if(!N(e[v][t])){e[v][t]=null;try{delete e[v][t]}catch(r){}return}H(e.subclass,function(e){e[v][t]&&N(e[v][t])&&N(e[v][t].__original_)&&K(e[v],t,e[v][t].__original_)}),e[v][t]=null;try{delete e[v][t]}catch(r){}},nt=function(){var e=Q,n={},r=[],i=arguments,s=i.length,o,u;return s===1?n=i[0]:s===2?(!i[0].__isclass_&&!C(i[0])?r=M(i[0]):e=i[0],n=i[1]):(e=i[0],r=M(i[1]),n=i[2]),n=F({},n),!e.__isclass_&&!n.init&&(n.init=e),o=function(){var e,n,r;if(!this||!this.init||!(this instanceof o))return o.invoke.apply(o,arguments);for(n=0,r=X.length;n<r;n++){e=X[n].onInit.call(X[n],this,o);if(e!==t){if(T(e))throw Error("Return values during onInit hook can only be objects.");return e}}e=this.init.apply(this,arguments);if(e!==t){if(T(e))throw Error("Return values for the constructor can only be objects.");return e}},delete n.applicate,o.applicate=function(e){var t=function(){return o.apply(this,e)};return t[v]=o[v],new t},o.invoke=n.invoke||(e.invoke&&N(e.invoke)&&!e.invoke.__original_?e.invoke:null)||P(function(){return o.applicate(arguments)},!0),delete n.invoke,o.superclass=e,o.subclass=[],o.implement=(k(e.implement)?e.implement:[]).concat(r),u=o[v]=J(e[v]),o.extend=u.extend=function(){return nt.apply(null,[o].concat(_(arguments)))},e.subclass&&k(e.subclass)&&e.subclass.push(o),n.invoke=function(t,n){if(t in e[v]&&t!=="invoke"&&N(e[v][t])){var r=this.invoke,i;return this.invoke=e[v].invoke,i=e[v][t].apply(this,n||[]),this.invoke=r,i}throw Error('Function "'+t+'" of parent class being invoked is undefined.')},o.addProperty=function(n,r,i){return i=i||"",r===t&&typeof n!="string"?H(O(n),function(t){et(o,e,i+t,n[t])}):et(o,e,i+n,r),o},o.removeProperty=function(e){return tt(o,e),o},r.length!==0&&H(r,function(e){var t=e.__isclass_?e[v]:e;H(O(t),function(e){!S.call(u,e)&&!S.call(n,e)&&(n[e]=t[e])})}),H(U,function(t){t.onCreate.call(t,o,e)}),o.addProperty(n),u.constructor=o,u.self=o,o.__isclass_=!0,o};Y("static",{onCreate:function(e){var t=this.propPrefix;e.addStaticProperty=function(n,r){return e.addProperty(n,r,t)},e.removeStaticProperty=function(n){return e.removeProperty(t+n)}},onPropAdd:function(e,t,n,r){if(I.test(n))return;K(e,n,N(r)&&!r.__isclass_?P(function(){return r.apply(e,arguments)},r):r)},onPropRemove:function(e,t){if(I.test(t))return;e[t]=null;try{delete e[t]}catch(n){}}}),Y("nowrap",{onCreate:function(e){var t=this.propPrefix;e.addUnwrappedProperty=function(n,r){return e.addProperty(n,r,t)}},onPropAdd:function(e,t,n,r){K(e[v],n,r)}}),Y("alias",{onCreate:function(e){var t=this.propPrefix;e.addAliasedProperty=function(n,r){return e.addProperty(n,r,t)}},onPropAdd:function(e,t,n,r){et(e,t,n,function(){return this[r].apply(this,arguments)})}}),Y("bind",{onCreate:function(e,t){var n=this;e.bindings=(t.bindings||[]).slice(0),e.addBoundProperty=function(t,r){return e.addProperty(t,r,n.propPrefix)},e.removeBoundProperty=function(t){return e.removeProperty(n.propPrefix+t)}},onPropAdd:function(e,t,n,r){var i=D(e.bindings,n);i<0&&N(r)&&!r.__isclass_&&e.bindings.push(n),et(e,t,n,r)},onPropRemove:function(e,t){var n=D(e.bindings,t);if(n<0)return;e.bindings.splice(n,1),H(e.subclass,function(e){D(e.bindings,t)>-1&&!S.call(e[v],t)&&e.removeBoundProperty(t)}),tt(e,t)},onInit:function(e,t){var n=t.bindings||null;if(n===null||n.length===0)return;H(n,function(n){K(e,n,function(){var r=_(arguments);return r.unshift(this),t[v][n].apply(e,r)})})}}),n=nt({init:function(e,t,n){if(!e)throw Error("Cannot create Observer without class context.");this.context=e,this.name=t,this.events=[],this.writable=!0,this.delay=0,this._debounce=null,n!==null&&typeof n=="object"?(this.getter=N(n.get)?n.get:null,this.setter=N(n.set)?n.set:null,this.value=n.value,this.writable=typeof n.writable=="boolean"?n.writable:!0,this.delay=typeof n.delay=="number"?n.delay:0):this.value=n},get:function(){return this.getter?this.getter.call(this.context,this.value):this.value},set:function(e){var t=this.value,n=this.context;return this.writable?(this.value=this.setter?this.setter.call(n,e,t):e,this.value!==t&&this.emit(),n):n},emit:function(){var t=this,n=_(arguments);return this.delay>0?(this._debounce!==null&&e.clearTimeout(this._debounce),this._debounce=e.setTimeout(function(){t._debounce=null,t._triggerEmit(n)},this.delay)):this._triggerEmit(n),this.context},_triggerEmit:function(e){var t=0,n=this.events.length,r=this.context,i=this.events;e.unshift(this.value);for(;t<n;++t)i[t].apply(r,e)},addListener:function(e){if(!N(e))throw Error("Observer.addListener only takes instances of Function");return this.events[this.events.length]=e,this.context},once:function(e){if(!N(e))throw Error("Observer.once only takes instances of Function");var t=this,n=function(){t.removeListener(n),e.apply(this,arguments)};return n.listener=e,this.addListener(n),this.context},removeListener:function(e){if(!N(e))throw Error("Observer.removeListener only takes instances of Function");var t=this.context,n=this.events,r=-1,i=0,s=n.length;for(;i<s;++i)if(n[i]===e||n[i].listener&&n[i].listener===e){r=i;break}return r<0?t:(n.splice(i,1),t)},removeAllListeners:function(){return this.events=null,this.events=[],this.context},listeners:function(){return this.events},toValue:function(){return this.value&&this.value.toValue?this.value.toValue():this.value},toString:function(){return"[observer "+this.name+"]"}}),n[v].on=n[v].addListener,m.Observer=n,Y("observable",{onCreate:function(e,t){var n=this;e.observable=F({},t.observable||{}),e.addObservableProperty=function(t,r){return e.addProperty(t,r,n.propPrefix)},e.removeObservableProperty=function(t){return e.removeProperty(n.propPrefix+t)}},onPropAdd:function(e,t,n,r){e.observable[n]=r,K(e[v],n,null),H(e.subclass,function(e){S.call(e.observable,n)||e.addObservableProperty(n,r)})},onPropRemove:function(e,t){var n=e.observable[t];H(e.subclass,function(e){e.observable[t]===n&&e.removeObservableProperty(t)}),e.observable[t]=null;try{delete e.observable[t]}catch(r){}},onInit:function(e,t){var r,i=t.observable||null;if(i===null)return;for(r in i)S.call(i,r)&&(e[r]=new n(e,r,i[r]))}}),r={},i="GLOBAL",f=function(e,t){return H(e,function(e){t[e]||(t[e]={}),t=t[e]}),t},l=function(e,t,n){if(typeof t=="string"){n=e[t]||a().get(t)||null;if(!n)throw Error("Invalid parent class ["+t+"] specified.");return n}return k(t)?B(t,function(t){return l(e,t)}):t},c=nt({init:function(e){this.ref={},this.name=e},create:function(){var e=_(arguments),t=e.shift(),n=t.split("."),r=n.pop(),i=this,s=f(n,i),o=i.ref,u=nt.apply(null,B(e,function(e){return l(o,e)}));return u._name_=r,u._namespace_=t,u.extend=u[v].extend=function(){return nt.apply(null,[u].concat(B(arguments,function(e){return l(o,e)})))},u.getNamespace=function(){return i},u.toString=function(){return"[object "+t+"]"},s[r]=u,o[t]=u,u},destroy:function(e){var t,n=e.split("."),r=n.pop(),i=this,s=this,o=this.ref;return H(n,function(e){if(!s[e])return s=null,!1;s=s[e]}),s?(t=s[r],t?(H(t.subclass,function(e){i.destroy(e._namespace_)}),t.superclass.subclass&&(t.superclass.subclass=j(t.superclass.subclass,t)),H(o,function(t,n){n!==e&&n.indexOf(e)===0&&i.destroy(t._namespace_)}),delete s[r],delete o[e],this):this):this},exists:function(e){return!!this.ref[e]},get:function(e,t){return N(t)?(this.ref[e]?t(this.ref[e]):this.load(e,t),this):this.ref[e]||this.name!==i&&a().get(e)||null},load:function(e,t){return t&&t(this.ref[e]||null),this},setAutoloader:function(e){if(!N(e))throw Error("Namespace.setAutoloader only takes instances of Function");return this.load=e,this},getName:function(){return this.name},toString:function(){return"[namespace "+this.name+"]"}}),s=function(e){return e instanceof c?e:(e=e||i,r[e]||(r[e]=new c(e)),r[e])},o=function(e){e instanceof c&&(e=e.name);if(e===i)return;delete r[e]},u=function(e){var t,n=e.split("."),i=n.length;while((t=n.slice(0,i--).join("."))!=="")if(r[t])return r[t];return null},a=function(){return s(i)},F(m,{Namespace:c,getNamespace:s,destroyNamespace:o,testNamespace:u,getGlobalNamespace:a,global:a(),provide:function(t,n){return f(t.split("."),n||e||{})}}),h="/",p=nt({invoke:function(){var e=_(arguments),t=e.length,n,r;if(t===0)return s();if(typeof e[0]=="string")return t===1?(r=e[0].split(h),n=s(r[0]),r[1]?n.get(r[1]):n):t===2&&typeof e[1]=="string"?s(e[0]).get(e[1]):(r=e.shift().split(h),n=s(r[0]),r[1]&&e.unshift(r[1]),n.create.apply(n,e));return nt.apply(null,e)},init:function(){var e=_(arguments),t,n;if(e.length<1)throw Error("Classify object cannot be instantiated!");return t=k(e[e.length-1])?e.pop():[],n=p.invoke.apply(null,e),n.__isclass_?n.applicate(t):n}}),F(p,m,{version:"0.10.1",create:nt,addMutator:Y,removeMutator:Z,extend:F}),typeof module!="undefined"&&module.exports?(module.exports=p,p.Classify=p):typeof e.define=="function"&&e.define.amd?e.define("classify",function(){return p}):(d=e.Classify,e.Classify=p,p.noConflict=function(){return d===t?delete e.Classify:e.Classify=d,p})})(this);
+ * Date: Fri, 08 Mar 2013 21:52:25 GMT
+ */;(function(e,t){"use strict";var n,r,i,s,o,u,a,f,l,c,h,p,d,v="prototype",m={},g=!{toString:null}.propertyIsEnumerable("toString"),y=g?"hasOwnProperty,valueOf,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,constructor".split(","):[],b=y.length,w=Object[v],E=w.toString,S=w.hasOwnProperty,x=/^(?:boolean|number|string|undefined)$/,T=function(e){return e===null||x.test(typeof e)},N=function(e){return E.call(e)==="[object Function]"},C=function(e){return e&&e[v]&&N(e)},k=Array.isArray||function(e){return E.call(e)==="[object Array]"},L=/^\s*function\s+.+?\(.*?\)\s*\{\s*\[native code\]\s*\}\s*$/,A=function(e){return N(e)&&L.test(e+"")},O=function(e){var t=[],n;for(n in e)t[t.length]=n;if(g)for(n=0;n<b;++n)S.call(e,y[n])&&(t[t.length]=y[n]);return t},M=function(e){return k(e)?e:[e]},_=function(e){return Array[v].slice.call(e,0)},D=Array[v].indexOf?function(e,t){return e.indexOf(t)}:function(e,t){var n=0,r=e.length;for(;n<r;++n)if(e[n]===t)return n;return-1},P=function(e,t){return e.__original_=t,e},H=function(e,n,r){if(!e)return e;var i,s=0,o=e.length,u;if(o===t||N(e)){u=O(e),o=u.length;for(i=e[u[0]];s<o;i=e[u[++s]])if(n.call(r||i,i,u[s],e)===!1)break}else for(i=e[0];s<o;i=e[++s])if(n.call(r||i,i,s,e)===!1)break;return e},B=function(e,t){var n=[];return H(e,function(r,i){n[n.length]=t(r,i,e)}),n},j=function(e,t){var n=[];return H(e,function(e){e!==t&&(n[n.length]=e)}),n},F=function(){var e=_(arguments),t=e.shift();return H(e,function(e){H(O(e),function(n){t[n]=e[n]})}),t},I=/^(?:superclass|subclass|implement|observable|bindings|extend|prototype|applicate|(?:add|remove)(?:Static|Observable|Aliased|Bound)Property)$/,q=/^__/,R={},U=[],z=[],W=[],X=[],V=[U,z,W,X],$=["onCreate","onPropAdd","onPropRemove","onInit"],J=A(Object.create)?Object.create:function(e){var t=function(){};return t[v]=e,new t},K=function(e,t,n){e[t]=n},Q=function(){var e=function(){};return e[v].init=function(){},e.superclass=null,e.subclass=[],e.implement=[],e[v].constructor=Q,e[v].self=Q,e.__isclass_=!0,e}(),G=function(e,n){return P(function(){var r=this.parent,i;return this.parent=e,i=n.apply(this,arguments),r===t?delete this.parent:this.parent=r,i},n)},Y=function(e,t){if(R[e])throw Error('Adding duplicate mutator "'+e+'".');R[e]=t,t.name=e,t.propTest=RegExp("^__"+e+"_"),t.propPrefix="__"+e+"_",H($,function(e,n){t[e]&&V[n].push(t)})},Z=function(e){var t=R[e];if(!t)throw Error("Removing unknown mutator.");H($,function(e,n){if(t[e]){var r=D(V[n],t);r>-1&&V[n].splice(r,1)}}),R[e]=null;try{delete R[e]}catch(n){}},et=function(e,t,n,r){var i,s,o;if(r===w[n])return;if(q.test(n)){i=!1,H(z,function(s){if(s.propTest.test(n))return n===s.propPrefix?H(r,function(n,r){s.onPropAdd.call(s,e,t,r,n)}):s.onPropAdd.call(s,e,t,n.replace(s.propTest,""),r),i=!0,!1});if(i)return}s=t[v][n],o=e[v],K(o,n,N(r)&&!r.__isclass_&&N(s)?G(s,r):r),N(r)&&H(e.subclass,function(e){N(e[v][n])&&!e[v][n].__original_&&K(e[v],n,G(o[n],e[v][n]))})},tt=function(e,t){var n=!1;if(q.test(t)){H(W,function(r){if(r.propTest.test(t))return r.onPropRemove.call(r,e,t.replace(r.propTest,"")),n=!0,!1});if(n)return}if(!N(e[v][t])){e[v][t]=null;try{delete e[v][t]}catch(r){}return}H(e.subclass,function(e){e[v][t]&&N(e[v][t])&&N(e[v][t].__original_)&&K(e[v],t,e[v][t].__original_)}),e[v][t]=null;try{delete e[v][t]}catch(r){}},nt=function(){var e=Q,n={},r=[],i=arguments,s=i.length,o,u;return s===1?n=i[0]:s===2?(!i[0].__isclass_&&!C(i[0])?r=M(i[0]):e=i[0],n=i[1]):(e=i[0],r=M(i[1]),n=i[2]),n=F({},n),!e.__isclass_&&!n.init&&(n.init=e),o=function(){var e,n,r;if(!this||!this.init||!(this instanceof o))return o.invoke.apply(o,arguments);for(n=0,r=X.length;n<r;n++){e=X[n].onInit.call(X[n],this,o);if(e!==t){if(T(e))throw Error("Return values during onInit hook can only be objects.");return e}}e=this.init.apply(this,arguments);if(e!==t){if(T(e))throw Error("Return values for the constructor can only be objects.");return e}},delete n.applicate,o.applicate=function(e){var t=function(){return o.apply(this,e)};return t[v]=o[v],new t},o.invoke=n.invoke||(e.invoke&&N(e.invoke)&&!e.invoke.__original_?e.invoke:null)||P(function(){return o.applicate(arguments)},!0),delete n.invoke,o.superclass=e,o.subclass=[],o.implement=(k(e.implement)?e.implement:[]).concat(r),u=o[v]=J(e[v]),o.extend=u.extend=function(){return nt.apply(null,[o].concat(_(arguments)))},e.subclass&&k(e.subclass)&&e.subclass.push(o),n.invoke=function(t,n){if(t in e[v]&&t!=="invoke"&&N(e[v][t])){var r=this.invoke,i;return this.invoke=e[v].invoke,i=e[v][t].apply(this,n||[]),this.invoke=r,i}throw Error('Function "'+t+'" of parent class being invoked is undefined.')},o.addProperty=function(n,r,i){return i=i||"",r===t&&typeof n!="string"?H(O(n),function(t){et(o,e,i+t,n[t])}):et(o,e,i+n,r),o},o.removeProperty=function(e){return tt(o,e),o},r.length!==0&&H(r,function(e){var t=e.__isclass_?e[v]:e;H(O(t),function(e){!S.call(u,e)&&!S.call(n,e)&&(n[e]=t[e])})}),H(U,function(t){t.onCreate.call(t,o,e)}),o.addProperty(n),u.constructor=o,u.self=o,o.__isclass_=!0,o};Y("static",{onCreate:function(e){var t=this.propPrefix;e.addStaticProperty=function(n,r){return e.addProperty(n,r,t)},e.removeStaticProperty=function(n){return e.removeProperty(t+n)}},onPropAdd:function(e,t,n,r){if(I.test(n))return;K(e,n,N(r)&&!r.__isclass_?P(function(){return r.apply(e,arguments)},r):r)},onPropRemove:function(e,t){if(I.test(t))return;e[t]=null;try{delete e[t]}catch(n){}}}),Y("nowrap",{onCreate:function(e){var t=this.propPrefix;e.addUnwrappedProperty=function(n,r){return e.addProperty(n,r,t)}},onPropAdd:function(e,t,n,r){K(e[v],n,r)}}),Y("alias",{onCreate:function(e){var t=this.propPrefix;e.addAliasedProperty=function(n,r){return e.addProperty(n,r,t)}},onPropAdd:function(e,t,n,r){et(e,t,n,function(){return this[r].apply(this,arguments)})}}),Y("bind",{onCreate:function(e,t){var n=this;e.bindings=(t.bindings||[]).slice(0),e.addBoundProperty=function(t,r){return e.addProperty(t,r,n.propPrefix)},e.removeBoundProperty=function(t){return e.removeProperty(n.propPrefix+t)}},onPropAdd:function(e,t,n,r){var i=D(e.bindings,n);i<0&&N(r)&&!r.__isclass_&&e.bindings.push(n),et(e,t,n,r)},onPropRemove:function(e,t){var n=D(e.bindings,t);if(n<0)return;e.bindings.splice(n,1),H(e.subclass,function(e){D(e.bindings,t)>-1&&!S.call(e[v],t)&&e.removeBoundProperty(t)}),tt(e,t)},onInit:function(e,t){var n=t.bindings||null;if(n===null||n.length===0)return;H(n,function(n){K(e,n,function(){var r=_(arguments);return r.unshift(this),t[v][n].apply(e,r)})})}}),n=nt({init:function(e,t,n){if(!e)throw Error("Cannot create Observer without class context.");this.context=e,this.name=t,this.events=[],this.writable=!0,this.delay=0,this._debounce=null,n!==null&&typeof n=="object"?(this.getter=N(n.get)?n.get:null,this.setter=N(n.set)?n.set:null,this.value=n.value,this.writable=typeof n.writable=="boolean"?n.writable:!0,this.delay=typeof n.delay=="number"?n.delay:0):this.value=n},get:function(){return this.getter?this.getter.call(this.context,this.value):this.value},set:function(e){var t=this.value,n=this.context;return this.writable?(this.value=this.setter?this.setter.call(n,e,t):e,this.value!==t&&this.emit(),n):n},emit:function(){var t=this,n=_(arguments);return this.delay>0?(this._debounce!==null&&e.clearTimeout(this._debounce),this._debounce=e.setTimeout(function(){t._debounce=null,t._triggerEmit(n)},this.delay)):this._triggerEmit(n),this.context},_triggerEmit:function(e){var t=0,n=this.events.length,r=this.context,i=this.events;e.unshift(this.value);for(;t<n;++t)i[t].apply(r,e)},addListener:function(e){if(!N(e))throw Error("Observer.addListener only takes instances of Function");return this.events[this.events.length]=e,this.context},once:function(e){if(!N(e))throw Error("Observer.once only takes instances of Function");var t=this,n=function(){t.removeListener(n),e.apply(this,arguments)};return n.listener=e,this.addListener(n),this.context},removeListener:function(e){if(!N(e))throw Error("Observer.removeListener only takes instances of Function");var t=this.context,n=this.events,r=-1,i=0,s=n.length;for(;i<s;++i)if(n[i]===e||n[i].listener&&n[i].listener===e){r=i;break}return r<0?t:(n.splice(i,1),t)},removeAllListeners:function(){return this.events=null,this.events=[],this.context},listeners:function(){return this.events},toValue:function(){return this.value&&this.value.toValue?this.value.toValue():this.value},toString:function(){return"[observer "+this.name+"]"}}),n[v].on=n[v].addListener,m.Observer=n,Y("observable",{onCreate:function(e,t){var n=this;e.observable=F({},t.observable||{}),e.addObservableProperty=function(t,r){return e.addProperty(t,r,n.propPrefix)},e.removeObservableProperty=function(t){return e.removeProperty(n.propPrefix+t)}},onPropAdd:function(e,t,n,r){e.observable[n]=r,K(e[v],n,null),H(e.subclass,function(e){S.call(e.observable,n)||e.addObservableProperty(n,r)})},onPropRemove:function(e,t){var n=e.observable[t];H(e.subclass,function(e){e.observable[t]===n&&e.removeObservableProperty(t)}),e.observable[t]=null;try{delete e.observable[t]}catch(r){}},onInit:function(e,t){var r,i=t.observable||null;if(i===null)return;for(r in i)S.call(i,r)&&(e[r]=new n(e,r,i[r]))}}),r={},i="GLOBAL",f=function(e,t){return H(e,function(e){t[e]||(t[e]={}),t=t[e]}),t},l=function(e,t,n){if(typeof t=="string"){n=e.get(t);if(!n)throw Error("Invalid parent class ["+t+"] specified.");return n}return k(t)?B(t,function(t){return l(e,t)}):t},c=nt({init:function(e){this.ref={},this.name=e},create:function(){var e=_(arguments),t=e.shift(),n=t.split("."),r=n.pop(),i=this,s=f(n,i),o=nt.apply(null,B(e,function(e){return l(i,e)}));return o._name_=r,o._namespace_=t,o.extend=o[v].extend=function(){return nt.apply(null,[o].concat(B(arguments,function(e){return l(i,e)})))},o.getNamespace=function(){return i},o.toString=function(){return"[object "+t+"]"},s[r]=o,i.ref[t]=o,o},destroy:function(e){var t,n=e.split("."),r=n.pop(),i=this,s=this,o=this.ref;return H(n,function(e){if(!s[e])return s=null,!1;s=s[e]}),s?(t=s[r],t?(H(t.subclass,function(e){i.destroy(e._namespace_)}),t.superclass.subclass&&(t.superclass.subclass=j(t.superclass.subclass,t)),H(o,function(t,n){n!==e&&n.indexOf(e)===0&&i.destroy(t._namespace_)}),delete s[r],delete o[e],this):this):this},exists:function(e){return!!this.ref[e]},get:function(e){var t;return this.ref[e]?this.ref[e]:(t=this.load(e),t!==null?t:this.name!==i?a().get(e):null)},load:function(e,t){return this.ref[e]||null},setAutoloader:function(e){if(!N(e))throw Error("Namespace.setAutoloader only takes instances of Function");return this.load=e,this},getName:function(){return this.name},toString:function(){return"[namespace "+this.name+"]"}}),s=function(e){return e instanceof c?e:(e=e||i,r[e]||(r[e]=new c(e)),r[e])},o=function(e){e instanceof c&&(e=e.name);if(e===i)return;delete r[e]},u=function(e){var t,n=e.split("."),i=n.length;while((t=n.slice(0,i--).join("."))!=="")if(r[t])return r[t];return null},a=function(){return s(i)},F(m,{Namespace:c,getNamespace:s,destroyNamespace:o,testNamespace:u,getGlobalNamespace:a,global:a(),provide:function(t,n){return f(t.split("."),n||e||{})}}),h="/",p=nt({invoke:function(){var e=_(arguments),t=e.length,n,r;if(t===0)return s();if(typeof e[0]=="string")return t===1?(r=e[0].split(h),n=s(r[0]),r[1]?n.get(r[1]):n):t===2&&typeof e[1]=="string"?s(e[0]).get(e[1]):(r=e.shift().split(h),n=s(r[0]),r[1]&&e.unshift(r[1]),n.create.apply(n,e));return nt.apply(null,e)},init:function(){var e=_(arguments),t,n;if(e.length<1)throw Error("Classify object cannot be instantiated!");return t=k(e[e.length-1])?e.pop():[],n=p.invoke.apply(null,e),n.__isclass_?n.applicate(t):n}}),F(p,m,{version:"0.10.2",create:nt,addMutator:Y,removeMutator:Z,extend:F}),typeof define=="function"&&define.amd&&define(function(){return p}),typeof module!="undefined"&&module.exports?(module.exports=p,p.Classify=p):(d=e.Classify,e.Classify=p,p.noConflict=function(){return d===t?delete e.Classify:e.Classify=d,p})})(this);
View
8 docs/classify.docs.js
@@ -464,29 +464,25 @@ Classify.Namespace.prototype.exists = function(classname) {
*
* @param {String}
* name The name of the class to retrieve
- * @param {Function}
- * [callback] If passed in the first parameter will the found class
* @memberOf Classify.Namespace
* @return {Classify.Class}
* @type {Classify.Class}
* @refexample Classify.Namespace.prototype.get
*/
-Classify.Namespace.prototype.get = function(name, callback) {
+Classify.Namespace.prototype.get = function(name) {
return Classify.Class;
};
/**
* Default loader function that loads the internal classes from
*
* @param {String}
* name The name of the class to load
- * @param {Function}
- * callback The function to call when the class has loaded
* @memberOf Classify.Namespace
* @returns {Classify.Namespace}
* @type {Classify.Namespace}
* @refexample Classify.Namespace.prototype.load
*/
-Classify.Namespace.prototype.load = function(name, callback) {
+Classify.Namespace.prototype.load = function(name) {
return new Classify.Namespace();
};
/**
View
2 package.json
@@ -15,7 +15,7 @@
"abstraction"
],
"author": "Wei Kin Huang <wei@closedinterval.com>",
- "version": "0.10.0",
+ "version": "0.10.2",
"homepage": "http://classifyjs.com",
"repository": {
"type": "git",
View
18 src/export.js
@@ -68,19 +68,21 @@ extend(Classify, exportNames, {
extend : extend
});
-// Export the Classify object for **CommonJS**, with backwards-compatibility for the
-// old "require()" API. If we're not in CommonJS, add "Classify" to the global object.
-if (typeof module !== "undefined" && module.exports) {
- module.exports = Classify;
- // create a circular reference
- Classify.Classify = Classify;
-} else if (typeof root.define === "function" && root.define.amd) {
+/*global define */
+if (typeof define === "function" && define.amd) {
// Export Classify as an AMD module only if there is a AMD module loader,
// use lowercase classify, because AMD modules are usually loaded with filenames
// and Classify would usually be loaded with lowercase classify.js
- root.define("classify", function() {
+ define(function() {
return Classify;
});
+}
+if (typeof module !== "undefined" && module.exports) {
+ // Export the Classify object for **CommonJS**, with backwards-compatibility for the
+ // old "require()" API. If we're not in CommonJS, add "Classify" to the global object.
+ module.exports = Classify;
+ // create a circular reference
+ Classify.Classify = Classify;
} else {
// store previous value of root.Classify
var root_value = root.Classify;
View
47 src/namespace.js
@@ -16,10 +16,10 @@ provide = function(namespace, base) {
return base;
},
// ability to de-reference string into it's classes
-dereference = function(base, arg, ref) {
+dereference = function(ns, arg, ref) {
// Allow parent classes to be passed in as a string for lookup
if (typeof arg === "string") {
- ref = base[arg] || getGlobalNamespace().get(arg) || null;
+ ref = ns.get(arg);
if (!ref) {
throw new Error("Invalid parent class [" + arg + "] specified.");
}
@@ -28,7 +28,7 @@ dereference = function(base, arg, ref) {
// if we have an object, then that's what we want, otherwise arrays
// we need to loop through and convert them to the proper objects
return !isArray(arg) ? arg : map(arg, function(prop) {
- return dereference(base, prop);
+ return dereference(ns, prop);
});
};
@@ -51,20 +51,18 @@ var Namespace = create({
self = this,
// Retrieve the namespaced container
ref = provide(namespace, self),
- // Create a reference to the master classes array
- deref = self.ref,
// fix the arguments & create the class
c = create.apply(null, map(args, function(v) {
- return dereference(deref, v);
+ return dereference(self, v);
}));
// Assign the magic properties of the class's name and namespace
c._name_ = name;
c._namespace_ = fullname;
// fix the issue with the extends function referencing string classes
c.extend = c.prototype.extend = function() {
return create.apply(null, [ c ].concat(map(arguments, function(v) {
- return dereference(deref, v);
+ return dereference(self, v);
})));
};
// give classes the ability to always store the namespace for chaining
@@ -76,7 +74,7 @@ var Namespace = create({
};
// Assign the classes to the namespaced references
ref[name] = c;
- deref[fullname] = c;
+ self.ref[fullname] = c;
// Return the new class
return c;
},
@@ -135,24 +133,27 @@ var Namespace = create({
exists : function(classname) {
return !!this.ref[classname];
},
- get : function(name, callback) {
- // ability to load a class async if a callback is passed in
- if (isFunction(callback)) {
- if (this.ref[name]) {
- callback(this.ref[name]);
- } else {
- this.load(name, callback);
- }
- return this;
+ get : function(name) {
+ var tmp;
+ // already defined, return it
+ if (this.ref[name]) {
+ return this.ref[name];
+ }
+ // use the autoloader if defined
+ tmp = this.load(name);
+ // return if loaded
+ if (tmp !== null) {
+ return tmp;
}
- // otherwise just return the class if it is already avaliable or reach into the global namespace
- return this.ref[name] || (this.name !== global_namespace && getGlobalNamespace().get(name)) || null;
+ // reach into the global namespace
+ if (this.name !== global_namespace) {
+ return getGlobalNamespace().get(name);
+ }
+ // no class found
+ return null;
},
load : function(name, callback) {
- if (callback) {
- callback(this.ref[name] || null);
- }
- return this;
+ return this.ref[name] || null;
},
setAutoloader : function(callback) {
// make sure the callback is a function
View
42 test/namespace.js
@@ -1,31 +1,25 @@
QUnit.module("namespace");
QUnit.test("retrieval and creation", function() {
- QUnit.expect(8);
+ QUnit.expect(6);
var ns = getNamespace("Namespace1");
QUnit.ok(ns instanceof Namespace, "getNamespace returned a namespace");
QUnit.equal(ns, getNamespace("Namespace1"), "multiple calls to getNamespace returns the same object");
QUnit.equal(ns.getName(), "Namespace1", "the name of the current namespace is stored");
QUnit.equal(ns.get("A"), null, "get class with no callback returns the class for use");
- QUnit.equal(ns.get("A", function(c) {
- QUnit.equal(c, null, "async call to get returns null as class doesn't yet exist");
- }), ns, "get class returns the namespace for chaining and gives ability to load up a class in a async manner");
QUnit.equal(getNamespace(ns), ns, "Passing a instance of Namespace to getNamespace returns object as is.");
QUnit.equal(ns + "", "[namespace Namespace1]", "Namespace toString returns [namespace Name]");
-
});
QUnit.test("class creation", function() {
QUnit.expect(13);
var ns = getNamespace("Namespace2");
QUnit.ok(!ns.exists("A"), "checking for existience of undefined class");
- ns.get("A", function(k) {
- QUnit.equal(k, null, "attempting to retieve a undefined class");
- });
+ QUnit.equal(ns.get("A"), null, "attempting to retieve a undefined class");
// creating a single class
var c = ns.create("A", {
@@ -39,9 +33,7 @@ QUnit.test("class creation", function() {
QUnit.ok(!!c.__isclass_, "class created is a class object");
QUnit.ok(new c() instanceof base, "class creation created by extending the base class");
QUnit.equal(ns.A(), "invoke", "class reference within namespace object can still be invoked");
- ns.get("A", function(k) {
- QUnit.equal(k, c, "class reference is stored in internal reference array");
- });
+ QUnit.equal(ns.get("A"), c, "class reference is stored in internal reference array");
QUnit.equal(c.getNamespace(), ns, "namespaced class has a getter for the current namespace");
QUnit.equal(c + "", "[object A]", "namespaced class has overriden toString method");
@@ -59,9 +51,7 @@ QUnit.test("class creation", function() {
QUnit.ok(new d() instanceof base, "class creation created by extending the base class");
QUnit.equal(ns.B.C, d, "class reference is stored directly within namespace object (nested)");
QUnit.equal(ns.B.C(), "invoke", "class reference within namespace object can still be invoked");
- ns.get("B.C", function(k) {
- QUnit.equal(k, d, "class reference is stored in internal reference array (nested)");
- });
+ QUnit.equal(ns.get("B.C"), d, "class reference is stored in internal reference array (nested)");
});
QUnit.test("class extension and implementation using named references", function() {
@@ -154,13 +144,9 @@ QUnit.test("removing named classes", function() {
// destroy a class namespace
ns.destroy("A");
QUnit.equal(typeof ns.A, "undefined", "removing a named class from the namespace");
- ns.get("A.C", function(k) {
- QUnit.equal(k, null, "removed leaf classes from branch namespace");
- });
+ QUnit.equal(ns.get("A.C"), null, "removed leaf classes from branch namespace");
QUnit.equal(typeof ns.F, "undefined", "removed class that extended a destroyed class");
- ns.get("F", function(k) {
- QUnit.equal(k, null, "removed branch namespace that inherited from a removed branch");
- });
+ QUnit.equal(ns.get("F"), null, "removed branch namespace that inherited from a removed branch");
// destroy an nested class
ns.destroy("G.a");
@@ -204,27 +190,21 @@ QUnit.test("class autoloading", function() {
QUnit.expect(5);
var ns = getNamespace("Namespace6");
- ns.get("A", function(k) {
- QUnit.equal(k, null, "attempting to retieve a undefined class");
- });
+ QUnit.equal(ns.get("A"), null, "attempting to retieve a undefined class");
// create temp class
var ca = ns.create("A", {});
var temp = {};
// setting the autoloader for a specific namespace
- ns.setAutoloader(function(name, callback) {
+ ns.setAutoloader(function(name) {
QUnit.equal(name, "B", "autoloader only being called if class doesn't exist.");
- callback(temp);
+ return temp;
});
// testing autoloader
- ns.get("B", function(k) {
- QUnit.equal(k, temp, "retrieving class through autoloader method");
- });
- ns.get("A", function(k) {
- QUnit.equal(k, ca, "retrieving class already existing class");
- });
+ QUnit.equal(ns.get("B"), temp, "retrieving class through autoloader method");
+ QUnit.equal(ns.get("A"), ca, "retrieving an already existing class");
// attempt to set autoloader to a non function
QUnit.raises(function(){

0 comments on commit 3968415

Please sign in to comment.