Skip to content

Instantly share code, notes, and snippets.

@hokaccha
Created July 25, 2012 14:58
Show Gist options
  • Save hokaccha/3176608 to your computer and use it in GitHub Desktop.
Save hokaccha/3176608 to your computer and use it in GitHub Desktop.
define namespace
// namespace
window.MyApp = {};
// define namespace
MyApp.define = function(name, fn) {
var parent = MyApp;
var parts = name.split('.');
parts.forEach(function(part, i) {
var isLast = parts.length === i + 1;
var isUndef = typeof parent[part] === 'undefined';
if (isLast) {
if (isUndef) {
parent[part] = fn();
}
else {
throw new Error(name + ' is already exist');
}
}
else {
if (isUndef) {
parent[part] = {};
}
parent = parent[part];
}
});
};
MyApp.define('Model.Base', function() {
return Backbone.Model.extend({
...
});
});
MyApp.define('Model.Item', function() {
return MyApp.Model.Base.extend({
...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment