Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

module system almost comply with CommonJS Modules/1.1.1 (only thing w…

…hich does not exist it the require since all modules are loaded on a global tree)
  • Loading branch information...
commit 7a7a8407a1f21bbbbb18621f3898aee88608152a 1 parent 6e6c482
@rsms authored
View
72 client/lib/index.js
@@ -120,15 +120,6 @@ window.oui.EventEmitter.prototype.addListener =
// -----------------------------------------------------------------------------
// Module loading and defining
-var moduleHTML = function(module, query){
- if (query) {
- query = module.$html.find(query).clone();
- query.ouiModule = module;
- return query;
- }
- return module.$html;
-};
-
function filterFragmentWrapperArgs(args, parentId) {
args = Array.prototype.slice.call(args);
// case: no id (the modules fragment)
@@ -163,9 +154,7 @@ window.__defm = function(name, root, html, fun) {
html = null;
}
- var k, v,
- module = new window.oui.EventEmitter(),
- namep = name.split('.'),
+ var k, v, namep = name.split('.'),
curr = root, n, i = 0, L = namep.length-1;
for ( ; i<L; i++) {
@@ -174,50 +163,40 @@ window.__defm = function(name, root, html, fun) {
curr[n] = {};
curr = curr[n];
}
+ var baseName = namep[i];
+ var module = {id: name};
- module.id = name;
-
// fragment accessor
+ var fragid = name.replace(/\./g, '/');
+ function moduleFragment(id) {
+ return window.fragment.apply(undefined,
+ filterFragmentWrapperArgs(arguments, fragid));
+ }
+ moduleFragment.template = function(id) {
+ return window.fragment.template.apply(undefined,
+ filterFragmentWrapperArgs(arguments, fragid));
+ };
if (window.fragment) {
- var fragid = name.replace(/\./g, '/');
- module.fragment = function(id) {
- return window.fragment.apply(undefined,
- filterFragmentWrapperArgs(arguments, fragid));
- };
- module.fragment.template = function(id) {
- return window.fragment.template.apply(undefined,
- filterFragmentWrapperArgs(arguments, fragid));
- };
Object.keys(window.fragment).forEach(function(k){
if (k !== 'template') {
- module.fragment[k] = window.fragment[k];
+ moduleFragment[k] = window.fragment[k];
}
});
}
- if (!html) {
- module.__html = function(query){ return moduleHTML(module, query); };
- $(function(){
- module.$html = jQuery('#'+module.id.replace(/\./g, '-'));
- });
- }
-
- // this, exports, __name, __html, __parent
- fun.call(module, module, module.id, module.__html, curr);
+ // Execute module
+ var moduleExports = {parent: curr || {}};
+ // this, exports, module, fragment
+ fun.call(moduleExports, moduleExports, module, moduleFragment);
- var mname = namep[i], parent = curr[mname];
+ // Add module to our tree
+ var parent = curr[baseName];
if (parent !== undefined) {
- var t = typeof parent;
- if (t === 'object' || t === 'function') {
- window.oui.mixin(parent, module);
- } else {
- for (k in module) {
- console.warn('tried to overwrite module "'+module.id+'"');
- break;
- }
- }
+ // mix into already existing module
+ window.oui.mixin(parent, moduleExports);
} else {
- curr[mname] = module;
+ // this is a new module (just the way we like it)
+ curr[baseName] = moduleExports;
}
};
@@ -234,8 +213,7 @@ if (window.JSON === undefined && $.toJSON && $.secureEvalJSON) {
// -----------------------------------------------------------------------------
// oui module
-
-__defm('oui', window, function(exports, __name, __html){
+(function(exports){
var EMPTYFUNC = function(){};
@@ -344,4 +322,4 @@ exports.capabilities = {
};
-});
+})(window.oui);
View
2  client/lib/oui/anchor.js
@@ -90,7 +90,7 @@ function onHashChange() {
try {
route.handler(params, exports.path, prevPath);
} catch (e) {
- console.error('['+__name+'] error when calling handler: '+(e.stack || e),
+ console.error('['+module.id+'] error when calling handler: '+(e.stack || e),
'handler =>', route.handler);
}
}
View
18 client/lib/oui/backend.js
@@ -95,7 +95,7 @@ exports.retry = function(action, callback) {
}
var again, backend, onend, self = this;
onend = function(prevArgs) {
- console.warn(__name+'.retry: all backends failed. action =>', action);
+ console.warn(module.id+'.retry: all backends failed. action =>', action);
if (callback) {
if (!$.isArray(prevArgs)) prevArgs = [];
if (prevArgs.length === 0) {
@@ -110,7 +110,7 @@ exports.retry = function(action, callback) {
if (retries === exports.backends.length)
return onend(prevArgs);
backend = exports.current();
- console.debug(__name+' trying '+backend.host+':'+backend.port+' for '+action);
+ console.debug(module.id+' trying '+backend.host+':'+backend.port+' for '+action);
action(backend, function(err, responseOrHTTPCode) {
var args = Array.prototype.slice.call(arguments);
if (typeof responseOrHTTPCode === 'object')
@@ -127,7 +127,7 @@ exports.retry = function(action, callback) {
// keep track of this backend since it seems to work.
oui.cookie.set('__oui_backend', b.host+':'+b.port);
}
- console.debug(__name+' forwarding response', args);
+ console.debug(module.id+' forwarding response', args);
if (callback) callback.apply(self, args);
});
};
@@ -172,7 +172,7 @@ exports.next = function() {
// sanity check
if (exports.backends.length === 0) {
- console.warn(__name+'.backends is empty');
+ console.warn(module.id+'.backends is empty');
return;
}
@@ -245,10 +245,10 @@ exports.setup = function() {
// sanity check
if (exports.backends.length === 0) {
if (sameOriginBackend) {
- console.warn(__name+' no available backends found -- forcingly adding same-origin');
+ console.warn(module.id+' no available backends found -- forcingly adding same-origin');
exports.backends = [sameOriginBackend];
} else {
- console.error(__name+' no available backends found');
+ console.error(module.id+' no available backends found');
return;
}
}
@@ -257,7 +257,7 @@ exports.setup = function() {
for (i=0;(b=exports.backends[i]);++i) {
b.port = b.port ? parseInt(b.port) : 80;
if (!b.host) {
- throw new Error(__name+': inconsistency error: backend without host specification');
+ throw new Error(module.id+': inconsistency error: backend without host specification');
}
if (b.path) b.path = '/'+b.path.replace(/^\/+|\/+$/g, '');
if (!b.url) b.url = jQuery.proxy(backend_url, b);
@@ -274,7 +274,7 @@ exports.setup = function() {
if (b.host === t[0] && b.port === t[1]) {
exports.currentIndex = i;
restored = true;
- console.debug(__name+' restored previously used backend from cookie: '+b.url());
+ console.debug(module.id+' restored previously used backend from cookie: '+b.url());
break;
}
}
@@ -289,7 +289,7 @@ exports.setup = function() {
exports.currentIndex = Math.round(Math.random()*hi);
if (oui.debug) {
b = exports.backends[exports.currentIndex];
- console.debug(__name+' selected a random backend: '+
+ console.debug(module.id+' selected a random backend: '+
(b ? b.url() : '<null>'));
}
}
View
2  client/lib/oui/graphics/piechart.js
@@ -93,4 +93,4 @@ PieChart.prototype.drawUnlessComplete = function(g) {
}
};
-__parent.PieChart = PieChart;
+exports.parent.PieChart = PieChart;
View
2  client/lib/oui/http.js
@@ -237,7 +237,7 @@ oui.inherits(exports.Request, oui.EventEmitter, {
},
toString: function(){
- return __name+'.Request<'+(this.id === undefined ? 'new' : this.id)+
+ return module.id+'.Request<'+(this.id === undefined ? 'new' : this.id)+
' '+this.method+' '+this.url+'>';
}
});
View
4 client/lib/oui/pipe/transport/websocket.js
@@ -5,10 +5,10 @@ exports.isUseable = function(){
};
exports.Transport = function(options) {
- __parent.Transport.call(this, options);
+ exports.parent.Transport.call(this, options);
};
-oui.mixin(exports.Transport.prototype, __parent.Transport.prototype, {
+oui.mixin(exports.Transport.prototype, exports.parent.Transport.prototype, {
connect: function(backend, callback) {
var self = this;
View
2  client/lib/oui/session.js
@@ -151,7 +151,7 @@ oui.inherits(exports.Session, oui.EventEmitter, {
var m = Object.merge3(prevUser, prevUser, this.user);
if (m.added || m.updated) {
// ^ no need to test for conflicts, as there can be none
- console.log(__name+': user info changed:', m);
+ console.log(module.id+': user info changed:', m);
this.emit('userinfo', prevUser, m.added, m.updated);
}
} else {
View
4 examples/example1/client/messages.html
@@ -1,7 +1,7 @@
<script type="text/javascript" charset="utf-8">
oui.app.on('start', function(ev){
// Add header
- var header = exports.fragment('header');
+ var header = fragment('header');
$('body').append(header);
// Enable creation form
@@ -27,7 +27,7 @@
});
// Setup messages
- var messageList = exports.fragment('list', {}/*, function(){
+ var messageList = fragment('list', {}/*, function(){
// this is run each time the fragment is rendered (i.e. on update())
this.find('a[href$=/delete]').click(function(ev){
removeMessage((/\/(\d+)\/delete$/.exec(this.href))[1]);
View
2  oui/builder/index.js
@@ -572,7 +572,7 @@ mixin(Source.prototype, {
this.content = this.content.replace(SOURCE_JSOPT_UNTANGLED_RE, '$1');
if (cl === this.content.length) {
this.content = '__defm('+JSON.stringify(this.name)+
- ', function(exports, __name, __html, __parent){'+
+ ', function(exports, module, fragment){'+
this.content.replace(/[\r\n][\t ]*$/,'\n')+
'});/*'+this.name+'*/\n';
}
Please sign in to comment.
Something went wrong with that request. Please try again.