Skip to content

Instantly share code, notes, and snippets.

@pavlo-yuriychuk
Created July 30, 2013 05:44
Show Gist options
  • Save pavlo-yuriychuk/6110513 to your computer and use it in GitHub Desktop.
Save pavlo-yuriychuk/6110513 to your computer and use it in GitHub Desktop.
Chaplin module loader implementation
define(["jquery"], function ($) {
"use strict";
function load(name, config) {
var deferred = $.Deferred();
require([name], function (Module) {
var instance = new Module();
instance.configure(config);
deferred.resolve(instance.export());
}, function (err) {
deferred.reject(err);
});
return deferred;
}
return {
load: load
}
});
define(["chaplin", "backbone"], function (Chaplin, Backbone) {
"use strict";
var Module = Backbone.Event.extend({
name: "Module",
public: [],
config: {},
toString: function () {
return this.name;
},
init: function () {
this.on("config-loaded", this.onConfigLoaded);
},
onConfigLoaded: function () {
},
configure: function (value) {
this.config = value;
this.trigger("config-loaded", this.config);
},
export: function () {
var result = {};
_.each(this.public.concat(["toString", "init", "configure"]), function (item) {
result[item] = this[item];
}, this);
return result;
}
});
return Module;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment