Created: 2012-03-23 19:05
Updated: 2017-06-25 20:37
License: mit

#Spaces -- Easier nested namespaces

Spaces provides a shorthand for creating and referencing namespaces.

##Without Spaces

var EM = EM || {};

EM.Gallery = EM.Gallery || {};
EM.Gallery.views = EM.Gallery.views || {};
EM.Gallery.views.account = EM.Gallery.views.account || {};
EM.Gallery.models == EM.Gallery.models || {};

##With Spaces




Instantiate a space with a base path. Create namespaces using the set method. Return namespaces using the get method.

(function () {
	// EM.Gallery
	var space = Spaces.create('EM.Gallery');

 	// EM.Gallery.views.account

	// EM.Gallery.models
	var models = space.set('models');

	models.UserModel = Backbone.Model.extend({
		defaults: {
			firstName: 'Eric',
			lastName: 'Matthys'

	var UserModel = space.get('models.UserModel');

// EM.Gallery is also a reference to the space, so we can use it outside the module

Set up aliases for long namespaces using the alias method. The second argument for the get method sets a flag to use the alias.

(function () {
	// EM.Gallery
	var space = Spaces.create('EM.Gallery');

	// EM.Gallery.a.really.long.namespace
	space.alias('long', 'a.really.long.namespace');

	var long = space.get('long', true);
	long.testing = true;

Use the noConflict method to return the Spaces object back to its original value.

var AltSpaces = Spaces.noConflict();


##License Spaces is licensed under the terms of the MIT License. See the MIT-LICENSE file for details.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more