Skip to content

Commit

Permalink
module system almost comply with CommonJS Modules/1.1.1 (only thing w…
Browse files Browse the repository at this point in the history
…hich does not exist it the require since all modules are loaded on a global tree)
  • Loading branch information
rsms committed Aug 5, 2010
1 parent 6e6c482 commit 7a7a840
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 65 deletions.
72 changes: 25 additions & 47 deletions client/lib/index.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}
};

Expand All @@ -234,8 +213,7 @@ if (window.JSON === undefined && $.toJSON && $.secureEvalJSON) {

// -----------------------------------------------------------------------------
// oui module

__defm('oui', window, function(exports, __name, __html){
(function(exports){

var EMPTYFUNC = function(){};

Expand Down Expand Up @@ -344,4 +322,4 @@ exports.capabilities = {
};


});
})(window.oui);
2 changes: 1 addition & 1 deletion client/lib/oui/anchor.js
Expand Up @@ -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);
}
}
Expand Down
18 changes: 9 additions & 9 deletions client/lib/oui/backend.js
Expand Up @@ -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) {
Expand All @@ -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')
Expand All @@ -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);
});
};
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand All @@ -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>'));
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/lib/oui/graphics/piechart.js
Expand Up @@ -93,4 +93,4 @@ PieChart.prototype.drawUnlessComplete = function(g) {
}
};

__parent.PieChart = PieChart;
exports.parent.PieChart = PieChart;
2 changes: 1 addition & 1 deletion client/lib/oui/http.js
Expand Up @@ -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+'>';
}
});
Expand Down
4 changes: 2 additions & 2 deletions client/lib/oui/pipe/transport/websocket.js
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion client/lib/oui/session.js
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions 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
Expand All @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion oui/builder/index.js
Expand Up @@ -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';
}
Expand Down

0 comments on commit 7a7a840

Please sign in to comment.