diff --git a/packages/ember-application/lib/system/application.js b/packages/ember-application/lib/system/application.js index db820688839..fb2c25f9add 100644 --- a/packages/ember-application/lib/system/application.js +++ b/packages/ember-application/lib/system/application.js @@ -276,20 +276,23 @@ var Application = Namespace.extend(DeferredMixin, { Ember.libraries.registerCoreLibrary('Handlebars' + (EmberHandlebars.compile ? '' : '-runtime'), EmberHandlebars.VERSION); Ember.libraries.registerCoreLibrary('jQuery', jQuery().jquery); - if ( Ember.LOG_VERSION ) { - Ember.LOG_VERSION = false; // we only need to see this once per Application#init + if (Ember.LOG_VERSION) { + // we only need to see this once per Application#init + Ember.LOG_VERSION = false; + var libs = Ember.libraries._registry; - var nameLengths = EnumerableUtils.map(Ember.libraries, function(item) { - return get(item, "name.length"); + var nameLengths = EnumerableUtils.map(libs, function(item) { + return get(item, 'name.length'); }); var maxNameLength = Math.max.apply(this, nameLengths); Ember.debug('-------------------------------'); - Ember.libraries.each(function(name, version) { - var spaces = new Array(maxNameLength - name.length + 1).join(" "); - Ember.debug([name, spaces, ' : ', version].join("")); - }); + for (var i = 0, l = libs.length; i < l; i++) { + var lib = libs[i]; + var spaces = new Array(maxNameLength - lib.name.length + 1).join(' '); + Ember.debug([lib.name, spaces, ' : ', lib.version].join('')); + } Ember.debug('-------------------------------'); } }, diff --git a/packages/ember-metal/lib/libraries.js b/packages/ember-metal/lib/libraries.js index 627cf73f882..c15115f4229 100644 --- a/packages/ember-metal/lib/libraries.js +++ b/packages/ember-metal/lib/libraries.js @@ -1,45 +1,70 @@ -// Provides a way to register library versions with ember. +import Ember from "ember-metal/core"; import { forEach, indexOf } from "ember-metal/enumerable_utils"; -var libraries = function() { - var _libraries = []; - var coreLibIndex = 0; +/** + Helper class that allows you to register your library with Ember. - var getLibrary = function(name) { - for (var i = 0; i < _libraries.length; i++) { - if (_libraries[i].name === name) { - return _libraries[i]; + Singleton created at `Ember.libraries`. + + @class Libraries + @constructor + @private +*/ +function Libraries() { + this._registry = []; + this._coreLibIndex = 0; +} + +Libraries.prototype = { + constructor: Libraries, + + _getLibraryByName: function(name) { + var libs = this._registry; + var count = libs.length; + + for (var i = 0; i < count; i++) { + if (libs[i].name === name) { + return libs[i]; } } - }; + }, - _libraries.register = function(name, version) { - if (!getLibrary(name)) { - _libraries.push({name: name, version: version}); - } - }; + register: function(name, version, isCoreLibrary) { + var index = this._registry.length; - _libraries.registerCoreLibrary = function(name, version) { - if (!getLibrary(name)) { - _libraries.splice(coreLibIndex++, 0, {name: name, version: version}); + if (!this._getLibraryByName(name)) { + if (isCoreLibrary) { + index = this._coreLibIndex++; + } + this._registry.splice(index, 0, { name: name, version: version }); + } else { + Ember.warn('Library "' + name + '" is already registered with Ember.'); } - }; + }, + + registerCoreLibrary: function(name, version) { + this.register(name, version, true); + }, - _libraries.deRegister = function(name) { - var lib = getLibrary(name); - if (lib) _libraries.splice(indexOf(_libraries, lib), 1); - }; + deRegister: function(name) { + var lib = this._getLibraryByName(name); + var index; - _libraries.each = function (callback) { - forEach(_libraries, function(lib) { + if (lib) { + index = indexOf(this._registry, lib); + this._registry.splice(index, 1); + } + }, + + each: function(callback) { + Ember.deprecate('Using Ember.libraries.each() is deprecated. Access to a list of registered libraries is currently a private API. If you are not knowingly accessing this method, your out-of-date Ember Inspector may be doing so.'); + forEach(this._registry, function(lib) { callback(lib.name, lib.version); }); - }; - - return _libraries; -}(); + } +}; -export default libraries; +export default Libraries; diff --git a/packages/ember-metal/lib/main.js b/packages/ember-metal/lib/main.js index 1c85ca69173..d1492036e0f 100644 --- a/packages/ember-metal/lib/main.js +++ b/packages/ember-metal/lib/main.js @@ -160,7 +160,7 @@ import { oneWay } from "ember-metal/binding"; import run from "ember-metal/run_loop"; -import libraries from "ember-metal/libraries"; +import Libraries from "ember-metal/libraries"; import isNone from 'ember-metal/is_none'; import isEmpty from 'ember-metal/is_empty'; import isBlank from 'ember-metal/is_blank'; @@ -311,7 +311,7 @@ Ember.isGlobalPath = isGlobalPath; Ember.run = run; -Ember.libraries = libraries; +Ember.libraries = new Libraries(); Ember.libraries.registerCoreLibrary('Ember', Ember.VERSION); Ember.isNone = isNone; diff --git a/packages/ember-metal/tests/libraries_test.js b/packages/ember-metal/tests/libraries_test.js index 57dc727b79c..8256ab83423 100644 --- a/packages/ember-metal/tests/libraries_test.js +++ b/packages/ember-metal/tests/libraries_test.js @@ -1,50 +1,93 @@ -import libraries from "ember-metal/libraries"; +/* globals EmberDev */ +import Libraries from "ember-metal/libraries"; -var libs = libraries; +var libs, registry; -test('Ember registers itself', function() { - equal(libs[0].name, "Ember"); +QUnit.module('Libraries registry', { + setup: function() { + libs = new Libraries(); + registry = libs._registry; + }, + + teardown: function() { + libs = null; + registry = null; + } }); test('core libraries come before other libraries', function() { - var l = libs.length; - - libs.register("my-lib", "2.0.0a"); - libs.registerCoreLibrary("DS", "1.0.0-beta.2"); + expect(2); - equal(libs[l].name, "DS"); - equal(libs[l+1].name, "my-lib"); + libs.register('my-lib', '2.0.0a'); + libs.registerCoreLibrary('DS', '1.0.0-beta.2'); - libs.deRegister("my-lib"); - libs.deRegister("DS"); + equal(registry[0].name, 'DS'); + equal(registry[1].name, 'my-lib'); }); test('only the first registration of a library is stored', function() { - var l = libs.length; + expect(3); + + libs.register('magic', 1.23); + libs.register('magic', 2.23); + + equal(registry[0].name, 'magic'); + equal(registry[0].version, 1.23); + equal(registry.length, 1); +}); + +test('attempting to register a library that is already registered warns you', function() { + if (EmberDev && EmberDev.runningProdBuild){ + ok(true, 'Logging does not occur in production builds'); + return; + } + + expect(1); - libs.register("magic", 1.23); - libs.register("magic", 2.23); - libs.register("magic", 3.23); + var oldWarn = Ember.warn; + libs.register('magic', 1.23); - equal(libs[l].name, "magic"); - equal(libs[l].version, 1.23); - equal(libs.length, l+1); + Ember.warn = function(msg, test) { + if (!test) { + equal(msg, 'Library "magic" is already registered with Ember.'); + } + }; - libs.deRegister("magic"); + // Should warn us + libs.register('magic', 2.23); + + Ember.warn = oldWarn; }); test('libraries can be de-registered', function() { - var l = libs.length; - - libs.register("lib1", "1.0.0b"); - libs.register("lib2", "1.0.0b"); - libs.register("lib3", "1.0.0b"); + expect(2); - libs.deRegister("lib1"); - libs.deRegister("lib3"); + libs.register('lib1', '1.0.0b'); + libs.register('lib2', '1.0.0b'); + libs.register('lib3', '1.0.0b'); - equal(libs[l].name, "lib2"); - equal(libs.length, l+1); + libs.deRegister('lib1'); + libs.deRegister('lib3'); - libs.deRegister("lib2"); + equal(registry[0].name, 'lib2'); + equal(registry.length, 1); }); + + +test('Libraries#each allows us to loop through each registered library (but is deprecated)', function() { + expect(5); + + var items = [{ name: 'lib1', version: '1.0.0' }, { name: 'lib2', version: '2.0.0' }]; + + for (var i = 0, l = items.length; i < l; i++) { + libs.register(items[i].name, items[i].version); + } + + expectDeprecation(function() { + libs.each(function (name, version) { + var expectedLib = items.shift(); + equal(expectedLib.name, name); + equal(expectedLib.version, version); + }); + }, 'Using Ember.libraries.each() is deprecated. Access to a list of registered libraries is currently a private API. If you are not knowingly accessing this method, your out-of-date Ember Inspector may be doing so.'); +}); \ No newline at end of file diff --git a/packages/ember-metal/tests/main_test.js b/packages/ember-metal/tests/main_test.js new file mode 100644 index 00000000000..e4fcf6c14d7 --- /dev/null +++ b/packages/ember-metal/tests/main_test.js @@ -0,0 +1,10 @@ +import Ember from "ember-metal/core"; + +QUnit.module('ember-metal/core/main'); + +test('Ember registers itself', function() { + var lib = Ember.libraries._registry[0]; + + equal(lib.name, 'Ember'); + equal(lib.version, Ember.VERSION); +}); \ No newline at end of file