Created
July 25, 2012 14:58
-
-
Save hokaccha/3176608 to your computer and use it in GitHub Desktop.
define namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyApp.define('Model.Base', function() { | |
return Backbone.Model.extend({ | |
... | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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