File: api/src/object-factory.js
/**
* Internal class designed to create ludoJS class instances.
* The global ludo.factory is an instance of this class
* @class ObjectFactory
*/
ludo.ObjectFactory = new Class({
namespaces:[],
classReferences : {},
/**
Creates an instance of a class by "type" attribute
@method create
@param {Object|ludo.Core} config
@return {ludo.Core} object
*/
create:function(config){
if(this.isClass(config))return config;
config.type = config.type || 'View';
if(this.classReferences[config.type] !== undefined){
return new this.classReferences[config.type](config);
}
var ludoJsObj = this.getInNamespace('ludo', config);
if(ludoJsObj)return ludoJsObj;
for(var i=0;i<this.namespaces.length;i++){
var obj = this.getInNamespace(this.namespaces[i], config);
if(obj)return obj;
}
ludo.util.log('Could not find class ' + config.type);
return undefined;
},
/**
Register a class for quick lookup. First argument is the value of the type attribute you want
to support. It is not required to call this for each class you create. The alternative is to
register a namespace by calling ludo.factory.registerNamespace('MyApp'). However, if you have a lot of
classes, it will increase performance by registering your classes. ludoJS will then know it instantly
and doesn't have to traverse the name space tree to find it.
@method registerClass
@param {String} typeName
@param {ludo.Core} classReference
@example
ludo.factory.createNamespace('MyApp');
MyApp.MyView = new Class({
Extends: ludo.View,
type: 'MyApp.MyView'
});
ludo.factory.register('MyApp.MyView', MyApp.MyView);
...
...
new ludo.View({
...
children:[{
type:'MyApp.MyView' // ludoJS now knows how to find this class
}]
});
*/
registerClass:function(typeName, classReference){
this.classReferences[typeName] = classReference;
},
/**
Method used to create global name space for your web applications.
This methods makes ludoJS aware of the namespace and register a global variable
window[ns] for it if it does not exists. It makes it possible for ludoJS to find
classes by type attribute.
@method ludo.factory.createNamespace
@param {String} ns
@example
ludo.factory.createNamespace('MyApp');
...
...
MyApp.MyClass = new Class({
Extends: ludo.View,
type : 'MyApp.MyClass'
});
var view = new ludo.View({
children:[{
type : 'MyApp.MyClass'
}]
});
Notice that "Namespace" is used as prefix in type attribute in the last snippet. For
standard ludoJS components, you could write type:"View". For views in your namespaces,
you should always use the syntax "Namespace.ClassName"
*/
createNamespace:function(ns){
if(window[ns] === undefined)window[ns] = {};
if(this.namespaces.indexOf(ns) === -1)this.namespaces.push(ns);
},
getInNamespace:function(ns, config){
var type = config.type.split(/\./g);
if(type[0] === ns)type.shift();
var obj = window[ns];
for(var i=0;i<type.length;i++){
if(obj[type[i]] !== undefined){
obj = obj[type[i]];
}else{
return undefined;
}
}
return new obj(config);
},
isClass:function(obj){
return obj && obj.initialize !== undefined;
}
});
ludo.factory = new ludo.ObjectFactory();