ember-intl
Version:
Ember toolbox for internationalization.
1,689 lines (1,350 loc) • 1.71 MB
JavaScript
/*!
* @overview Ember - JavaScript Application Framework
* @copyright Copyright 2011-2014 Tilde Inc. and contributors
* Portions Copyright 2006-2011 Strobe Inc.
* Portions Copyright 2008-2011 Apple Inc. All rights reserved.
* @license Licensed under MIT license
* See https://raw.github.com/emberjs/ember.js/master/LICENSE
* @version 1.10.0
*/
(function() {
var enifed, requireModule, eriuqer, requirejs, Ember;
(function() {
Ember = this.Ember = this.Ember || {};
if (typeof Ember === 'undefined') { Ember = {}; };
function UNDEFINED() { }
if (typeof Ember.__loader === 'undefined') {
var registry = {}, seen = {};
enifed = function(name, deps, callback) {
registry[name] = { deps: deps, callback: callback };
};
requirejs = eriuqer = requireModule = function(name) {
var s = seen[name];
if (s !== undefined) { return seen[name]; }
if (s === UNDEFINED) { return undefined; }
seen[name] = {};
if (!registry[name]) {
throw new Error('Could not find module ' + name);
}
var mod = registry[name];
var deps = mod.deps;
var callback = mod.callback;
var reified = [];
var exports;
var length = deps.length;
for (var i=0; i<length; i++) {
if (deps[i] === 'exports') {
reified.push(exports = {});
} else {
reified.push(requireModule(resolve(deps[i], name)));
}
}
var value = length === 0 ? callback.call(this) : callback.apply(this, reified);
return seen[name] = exports || (value === undefined ? UNDEFINED : value);
};
function resolve(child, name) {
if (child.charAt(0) !== '.') {
return child;
}
var parts = child.split('/');
var parentBase = name.split('/').slice(0, -1);
for (var i=0, l=parts.length; i<l; i++) {
var part = parts[i];
if (part === '..') { parentBase.pop(); }
else if (part === '.') { continue; }
else { parentBase.push(part); }
}
return parentBase.join('/');
}
requirejs._eak_seen = registry;
Ember.__loader = {
define: enifed,
require: eriuqer,
registry: registry
};
} else {
enifed = Ember.__loader.define;
requirejs = eriuqer = requireModule = Ember.__loader.require;
}
})();
enifed("backburner",
["backburner/utils","backburner/platform","backburner/binary-search","backburner/deferred-action-queues","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __exports__) {
"use strict";
var each = __dependency1__.each;
var isString = __dependency1__.isString;
var isFunction = __dependency1__.isFunction;
var isNumber = __dependency1__.isNumber;
var isCoercableNumber = __dependency1__.isCoercableNumber;
var wrapInTryCatch = __dependency1__.wrapInTryCatch;
var now = __dependency1__.now;
var needsIETryCatchFix = __dependency2__.needsIETryCatchFix;
var searchTimer = __dependency3__["default"];
var DeferredActionQueues = __dependency4__["default"];
var slice = [].slice;
var pop = [].pop;
var global = this;
function Backburner(queueNames, options) {
this.queueNames = queueNames;
this.options = options || {};
if (!this.options.defaultQueue) {
this.options.defaultQueue = queueNames[0];
}
this.instanceStack = [];
this._debouncees = [];
this._throttlers = [];
this._timers = [];
}
Backburner.prototype = {
begin: function() {
var options = this.options;
var onBegin = options && options.onBegin;
var previousInstance = this.currentInstance;
if (previousInstance) {
this.instanceStack.push(previousInstance);
}
this.currentInstance = new DeferredActionQueues(this.queueNames, options);
if (onBegin) {
onBegin(this.currentInstance, previousInstance);
}
},
end: function() {
var options = this.options;
var onEnd = options && options.onEnd;
var currentInstance = this.currentInstance;
var nextInstance = null;
// Prevent double-finally bug in Safari 6.0.2 and iOS 6
// This bug appears to be resolved in Safari 6.0.5 and iOS 7
var finallyAlreadyCalled = false;
try {
currentInstance.flush();
} finally {
if (!finallyAlreadyCalled) {
finallyAlreadyCalled = true;
this.currentInstance = null;
if (this.instanceStack.length) {
nextInstance = this.instanceStack.pop();
this.currentInstance = nextInstance;
}
if (onEnd) {
onEnd(currentInstance, nextInstance);
}
}
}
},
run: function(target, method /*, args */) {
var onError = getOnError(this.options);
this.begin();
if (!method) {
method = target;
target = null;
}
if (isString(method)) {
method = target[method];
}
var args = slice.call(arguments, 2);
// guard against Safari 6's double-finally bug
var didFinally = false;
if (onError) {
try {
return method.apply(target, args);
} catch(error) {
onError(error);
} finally {
if (!didFinally) {
didFinally = true;
this.end();
}
}
} else {
try {
return method.apply(target, args);
} finally {
if (!didFinally) {
didFinally = true;
this.end();
}
}
}
},
join: function(target, method /*, args */) {
if (this.currentInstance) {
if (!method) {
method = target;
target = null;
}
if (isString(method)) {
method = target[method];
}
return method.apply(target, slice.call(arguments, 2));
} else {
return this.run.apply(this, arguments);
}
},
defer: function(queueName, target, method /* , args */) {
if (!method) {
method = target;
target = null;
}
if (isString(method)) {
method = target[method];
}
var stack = this.DEBUG ? new Error() : undefined;
var length = arguments.length;
var args;
if (length > 3) {
args = new Array(length - 3);
for (var i = 3; i < length; i++) {
args[i-3] = arguments[i];
}
} else {
args = undefined;
}
if (!this.currentInstance) { createAutorun(this); }
return this.currentInstance.schedule(queueName, target, method, args, false, stack);
},
deferOnce: function(queueName, target, method /* , args */) {
if (!method) {
method = target;
target = null;
}
if (isString(method)) {
method = target[method];
}
var stack = this.DEBUG ? new Error() : undefined;
var length = arguments.length;
var args;
if (length > 3) {
args = new Array(length - 3);
for (var i = 3; i < length; i++) {
args[i-3] = arguments[i];
}
} else {
args = undefined;
}
if (!this.currentInstance) {
createAutorun(this);
}
return this.currentInstance.schedule(queueName, target, method, args, true, stack);
},
setTimeout: function() {
var l = arguments.length;
var args = new Array(l);
for (var x = 0; x < l; x++) {
args[x] = arguments[x];
}
var length = args.length,
method, wait, target,
methodOrTarget, methodOrWait, methodOrArgs;
if (length === 0) {
return;
} else if (length === 1) {
method = args.shift();
wait = 0;
} else if (length === 2) {
methodOrTarget = args[0];
methodOrWait = args[1];
if (isFunction(methodOrWait) || isFunction(methodOrTarget[methodOrWait])) {
target = args.shift();
method = args.shift();
wait = 0;
} else if (isCoercableNumber(methodOrWait)) {
method = args.shift();
wait = args.shift();
} else {
method = args.shift();
wait = 0;
}
} else {
var last = args[args.length - 1];
if (isCoercableNumber(last)) {
wait = args.pop();
} else {
wait = 0;
}
methodOrTarget = args[0];
methodOrArgs = args[1];
if (isFunction(methodOrArgs) || (isString(methodOrArgs) &&
methodOrTarget !== null &&
methodOrArgs in methodOrTarget)) {
target = args.shift();
method = args.shift();
} else {
method = args.shift();
}
}
var executeAt = now() + parseInt(wait, 10);
if (isString(method)) {
method = target[method];
}
var onError = getOnError(this.options);
function fn() {
if (onError) {
try {
method.apply(target, args);
} catch (e) {
onError(e);
}
} else {
method.apply(target, args);
}
}
// find position to insert
var i = searchTimer(executeAt, this._timers);
this._timers.splice(i, 0, executeAt, fn);
updateLaterTimer(this, executeAt, wait);
return fn;
},
throttle: function(target, method /* , args, wait, [immediate] */) {
var backburner = this;
var args = arguments;
var immediate = pop.call(args);
var wait, throttler, index, timer;
if (isNumber(immediate) || isString(immediate)) {
wait = immediate;
immediate = true;
} else {
wait = pop.call(args);
}
wait = parseInt(wait, 10);
index = findThrottler(target, method, this._throttlers);
if (index > -1) { return this._throttlers[index]; } // throttled
timer = global.setTimeout(function() {
if (!immediate) {
backburner.run.apply(backburner, args);
}
var index = findThrottler(target, method, backburner._throttlers);
if (index > -1) {
backburner._throttlers.splice(index, 1);
}
}, wait);
if (immediate) {
this.run.apply(this, args);
}
throttler = [target, method, timer];
this._throttlers.push(throttler);
return throttler;
},
debounce: function(target, method /* , args, wait, [immediate] */) {
var backburner = this;
var args = arguments;
var immediate = pop.call(args);
var wait, index, debouncee, timer;
if (isNumber(immediate) || isString(immediate)) {
wait = immediate;
immediate = false;
} else {
wait = pop.call(args);
}
wait = parseInt(wait, 10);
// Remove debouncee
index = findDebouncee(target, method, this._debouncees);
if (index > -1) {
debouncee = this._debouncees[index];
this._debouncees.splice(index, 1);
clearTimeout(debouncee[2]);
}
timer = global.setTimeout(function() {
if (!immediate) {
backburner.run.apply(backburner, args);
}
var index = findDebouncee(target, method, backburner._debouncees);
if (index > -1) {
backburner._debouncees.splice(index, 1);
}
}, wait);
if (immediate && index === -1) {
backburner.run.apply(backburner, args);
}
debouncee = [
target,
method,
timer
];
backburner._debouncees.push(debouncee);
return debouncee;
},
cancelTimers: function() {
var clearItems = function(item) {
clearTimeout(item[2]);
};
each(this._throttlers, clearItems);
this._throttlers = [];
each(this._debouncees, clearItems);
this._debouncees = [];
if (this._laterTimer) {
clearTimeout(this._laterTimer);
this._laterTimer = null;
}
this._timers = [];
if (this._autorun) {
clearTimeout(this._autorun);
this._autorun = null;
}
},
hasTimers: function() {
return !!this._timers.length || !!this._debouncees.length || !!this._throttlers.length || this._autorun;
},
cancel: function(timer) {
var timerType = typeof timer;
if (timer && timerType === 'object' && timer.queue && timer.method) { // we're cancelling a deferOnce
return timer.queue.cancel(timer);
} else if (timerType === 'function') { // we're cancelling a setTimeout
for (var i = 0, l = this._timers.length; i < l; i += 2) {
if (this._timers[i + 1] === timer) {
this._timers.splice(i, 2); // remove the two elements
if (i === 0) {
if (this._laterTimer) { // Active timer? Then clear timer and reset for future timer
clearTimeout(this._laterTimer);
this._laterTimer = null;
}
if (this._timers.length > 0) { // Update to next available timer when available
updateLaterTimer(this, this._timers[0], this._timers[0] - now());
}
}
return true;
}
}
} else if (Object.prototype.toString.call(timer) === "[object Array]"){ // we're cancelling a throttle or debounce
return this._cancelItem(findThrottler, this._throttlers, timer) ||
this._cancelItem(findDebouncee, this._debouncees, timer);
} else {
return; // timer was null or not a timer
}
},
_cancelItem: function(findMethod, array, timer){
var item, index;
if (timer.length < 3) { return false; }
index = findMethod(timer[0], timer[1], array);
if (index > -1) {
item = array[index];
if (item[2] === timer[2]) {
array.splice(index, 1);
clearTimeout(timer[2]);
return true;
}
}
return false;
}
};
Backburner.prototype.schedule = Backburner.prototype.defer;
Backburner.prototype.scheduleOnce = Backburner.prototype.deferOnce;
Backburner.prototype.later = Backburner.prototype.setTimeout;
if (needsIETryCatchFix) {
var originalRun = Backburner.prototype.run;
Backburner.prototype.run = wrapInTryCatch(originalRun);
var originalEnd = Backburner.prototype.end;
Backburner.prototype.end = wrapInTryCatch(originalEnd);
}
function getOnError(options) {
return options.onError || (options.onErrorTarget && options.onErrorTarget[options.onErrorMethod]);
}
function createAutorun(backburner) {
backburner.begin();
backburner._autorun = global.setTimeout(function() {
backburner._autorun = null;
backburner.end();
});
}
function updateLaterTimer(backburner, executeAt, wait) {
var n = now();
if (!backburner._laterTimer || executeAt < backburner._laterTimerExpiresAt || backburner._laterTimerExpiresAt < n) {
if (backburner._laterTimer) {
// Clear when:
// - Already expired
// - New timer is earlier
clearTimeout(backburner._laterTimer);
if (backburner._laterTimerExpiresAt < n) { // If timer was never triggered
// Calculate the left-over wait-time
wait = Math.max(0, executeAt - n);
}
}
backburner._laterTimer = global.setTimeout(function() {
backburner._laterTimer = null;
backburner._laterTimerExpiresAt = null;
executeTimers(backburner);
}, wait);
backburner._laterTimerExpiresAt = n + wait;
}
}
function executeTimers(backburner) {
var n = now();
var fns, i, l;
backburner.run(function() {
i = searchTimer(n, backburner._timers);
fns = backburner._timers.splice(0, i);
for (i = 1, l = fns.length; i < l; i += 2) {
backburner.schedule(backburner.options.defaultQueue, null, fns[i]);
}
});
if (backburner._timers.length) {
updateLaterTimer(backburner, backburner._timers[0], backburner._timers[0] - n);
}
}
function findDebouncee(target, method, debouncees) {
return findItem(target, method, debouncees);
}
function findThrottler(target, method, throttlers) {
return findItem(target, method, throttlers);
}
function findItem(target, method, collection) {
var item;
var index = -1;
for (var i = 0, l = collection.length; i < l; i++) {
item = collection[i];
if (item[0] === target && item[1] === method) {
index = i;
break;
}
}
return index;
}
__exports__["default"] = Backburner;
});
enifed("backburner.umd",
["./backburner"],
function(__dependency1__) {
"use strict";
var Backburner = __dependency1__["default"];
/* global define:true module:true window: true */
if (typeof enifed === 'function' && enifed.amd) {
enifed(function() { return Backburner; });
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = Backburner;
} else if (typeof this !== 'undefined') {
this['Backburner'] = Backburner;
}
});
enifed("backburner/binary-search",
["exports"],
function(__exports__) {
"use strict";
__exports__["default"] = function binarySearch(time, timers) {
var start = 0;
var end = timers.length - 2;
var middle, l;
while (start < end) {
// since timers is an array of pairs 'l' will always
// be an integer
l = (end - start) / 2;
// compensate for the index in case even number
// of pairs inside timers
middle = start + l - (l % 2);
if (time >= timers[middle]) {
start = middle + 2;
} else {
end = middle;
}
}
return (time >= timers[start]) ? start + 2 : start;
}
});
enifed("backburner/deferred-action-queues",
["./utils","./queue","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var each = __dependency1__.each;
var Queue = __dependency2__["default"];
function DeferredActionQueues(queueNames, options) {
var queues = this.queues = Object.create(null);
this.queueNames = queueNames = queueNames || [];
this.options = options;
each(queueNames, function(queueName) {
queues[queueName] = new Queue(queueName, options[queueName], options);
});
}
function noSuchQueue(name) {
throw new Error("You attempted to schedule an action in a queue (" + name + ") that doesn't exist");
}
DeferredActionQueues.prototype = {
schedule: function(name, target, method, args, onceFlag, stack) {
var queues = this.queues;
var queue = queues[name];
if (!queue) {
noSuchQueue(name);
}
if (onceFlag) {
return queue.pushUnique(target, method, args, stack);
} else {
return queue.push(target, method, args, stack);
}
},
flush: function() {
var queues = this.queues;
var queueNames = this.queueNames;
var queueName, queue, queueItems, priorQueueNameIndex;
var queueNameIndex = 0;
var numberOfQueues = queueNames.length;
var options = this.options;
while (queueNameIndex < numberOfQueues) {
queueName = queueNames[queueNameIndex];
queue = queues[queueName];
var numberOfQueueItems = queue._queue.length;
if (numberOfQueueItems === 0) {
queueNameIndex++;
} else {
queue.flush(false /* async */);
queueNameIndex = 0;
}
}
}
};
__exports__["default"] = DeferredActionQueues;
});
enifed("backburner/platform",
["exports"],
function(__exports__) {
"use strict";
// In IE 6-8, try/finally doesn't work without a catch.
// Unfortunately, this is impossible to test for since wrapping it in a parent try/catch doesn't trigger the bug.
// This tests for another broken try/catch behavior that only exhibits in the same versions of IE.
var needsIETryCatchFix = (function(e,x){
try{ x(); }
catch(e) { } // jshint ignore:line
return !!e;
})();
__exports__.needsIETryCatchFix = needsIETryCatchFix;
});
enifed("backburner/queue",
["./utils","exports"],
function(__dependency1__, __exports__) {
"use strict";
var isString = __dependency1__.isString;
function Queue(name, options, globalOptions) {
this.name = name;
this.globalOptions = globalOptions || {};
this.options = options;
this._queue = [];
this.targetQueues = Object.create(null);
this._queueBeingFlushed = undefined;
}
Queue.prototype = {
push: function(target, method, args, stack) {
var queue = this._queue;
queue.push(target, method, args, stack);
return {
queue: this,
target: target,
method: method
};
},
pushUniqueWithoutGuid: function(target, method, args, stack) {
var queue = this._queue;
for (var i = 0, l = queue.length; i < l; i += 4) {
var currentTarget = queue[i];
var currentMethod = queue[i+1];
if (currentTarget === target && currentMethod === method) {
queue[i+2] = args; // replace args
queue[i+3] = stack; // replace stack
return;
}
}
queue.push(target, method, args, stack);
},
targetQueue: function(targetQueue, target, method, args, stack) {
var queue = this._queue;
for (var i = 0, l = targetQueue.length; i < l; i += 4) {
var currentMethod = targetQueue[i];
var currentIndex = targetQueue[i + 1];
if (currentMethod === method) {
queue[currentIndex + 2] = args; // replace args
queue[currentIndex + 3] = stack; // replace stack
return;
}
}
targetQueue.push(
method,
queue.push(target, method, args, stack) - 4
);
},
pushUniqueWithGuid: function(guid, target, method, args, stack) {
var hasLocalQueue = this.targetQueues[guid];
if (hasLocalQueue) {
this.targetQueue(hasLocalQueue, target, method, args, stack);
} else {
this.targetQueues[guid] = [
method,
this._queue.push(target, method, args, stack) - 4
];
}
return {
queue: this,
target: target,
method: method
};
},
pushUnique: function(target, method, args, stack) {
var queue = this._queue, currentTarget, currentMethod, i, l;
var KEY = this.globalOptions.GUID_KEY;
if (target && KEY) {
var guid = target[KEY];
if (guid) {
return this.pushUniqueWithGuid(guid, target, method, args, stack);
}
}
this.pushUniqueWithoutGuid(target, method, args, stack);
return {
queue: this,
target: target,
method: method
};
},
invoke: function(target, method, args, _, _errorRecordedForStack) {
if (args && args.length > 0) {
method.apply(target, args);
} else {
method.call(target);
}
},
invokeWithOnError: function(target, method, args, onError, errorRecordedForStack) {
try {
if (args && args.length > 0) {
method.apply(target, args);
} else {
method.call(target);
}
} catch(error) {
onError(error, errorRecordedForStack);
}
},
flush: function(sync) {
var queue = this._queue;
var length = queue.length;
if (length === 0) {
return;
}
var globalOptions = this.globalOptions;
var options = this.options;
var before = options && options.before;
var after = options && options.after;
var onError = globalOptions.onError || (globalOptions.onErrorTarget &&
globalOptions.onErrorTarget[globalOptions.onErrorMethod]);
var target, method, args, errorRecordedForStack;
var invoke = onError ? this.invokeWithOnError : this.invoke;
this.targetQueues = Object.create(null);
var queueItems = this._queueBeingFlushed = this._queue.slice();
this._queue = [];
if (before) {
before();
}
for (var i = 0; i < length; i += 4) {
target = queueItems[i];
method = queueItems[i+1];
args = queueItems[i+2];
errorRecordedForStack = queueItems[i+3]; // Debugging assistance
if (isString(method)) {
method = target[method];
}
// method could have been nullified / canceled during flush
if (method) {
//
// ** Attention intrepid developer **
//
// To find out the stack of this task when it was scheduled onto
// the run loop, add the following to your app.js:
//
// Ember.run.backburner.DEBUG = true; // NOTE: This slows your app, don't leave it on in production.
//
// Once that is in place, when you are at a breakpoint and navigate
// here in the stack explorer, you can look at `errorRecordedForStack.stack`,
// which will be the captured stack when this job was scheduled.
//
invoke(target, method, args, onError, errorRecordedForStack);
}
}
if (after) {
after();
}
this._queueBeingFlushed = undefined;
if (sync !== false &&
this._queue.length > 0) {
// check if new items have been added
this.flush(true);
}
},
cancel: function(actionToCancel) {
var queue = this._queue, currentTarget, currentMethod, i, l;
var target = actionToCancel.target;
var method = actionToCancel.method;
var GUID_KEY = this.globalOptions.GUID_KEY;
if (GUID_KEY && this.targetQueues && target) {
var targetQueue = this.targetQueues[target[GUID_KEY]];
if (targetQueue) {
for (i = 0, l = targetQueue.length; i < l; i++) {
if (targetQueue[i] === method) {
targetQueue.splice(i, 1);
}
}
}
}
for (i = 0, l = queue.length; i < l; i += 4) {
currentTarget = queue[i];
currentMethod = queue[i+1];
if (currentTarget === target &&
currentMethod === method) {
queue.splice(i, 4);
return true;
}
}
// if not found in current queue
// could be in the queue that is being flushed
queue = this._queueBeingFlushed;
if (!queue) {
return;
}
for (i = 0, l = queue.length; i < l; i += 4) {
currentTarget = queue[i];
currentMethod = queue[i+1];
if (currentTarget === target &&
currentMethod === method) {
// don't mess with array during flush
// just nullify the method
queue[i+1] = null;
return true;
}
}
}
};
__exports__["default"] = Queue;
});
enifed("backburner/utils",
["exports"],
function(__exports__) {
"use strict";
var NUMBER = /\d+/;
function each(collection, callback) {
for (var i = 0; i < collection.length; i++) {
callback(collection[i]);
}
}
__exports__.each = each;// Date.now is not available in browsers < IE9
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
var now = Date.now || function() { return new Date().getTime(); };
__exports__.now = now;
function isString(suspect) {
return typeof suspect === 'string';
}
__exports__.isString = isString;function isFunction(suspect) {
return typeof suspect === 'function';
}
__exports__.isFunction = isFunction;function isNumber(suspect) {
return typeof suspect === 'number';
}
__exports__.isNumber = isNumber;function isCoercableNumber(number) {
return isNumber(number) || NUMBER.test(number);
}
__exports__.isCoercableNumber = isCoercableNumber;function wrapInTryCatch(func) {
return function () {
try {
return func.apply(this, arguments);
} catch (e) {
throw e;
}
};
}
__exports__.wrapInTryCatch = wrapInTryCatch;
});
enifed("calculateVersion",
[],
function() {
"use strict";
'use strict';
var fs = eriuqer('fs');
var path = eriuqer('path');
module.exports = function () {
var packageVersion = eriuqer('../package.json').version;
var output = [packageVersion];
var gitPath = path.join(__dirname,'..','.git');
var headFilePath = path.join(gitPath, 'HEAD');
if (packageVersion.indexOf('+') > -1) {
try {
if (fs.existsSync(headFilePath)) {
var headFile = fs.readFileSync(headFilePath, {encoding: 'utf8'});
var branchName = headFile.split('/').slice(-1)[0].trim();
var refPath = headFile.split(' ')[1];
var branchSHA;
if (refPath) {
var branchPath = path.join(gitPath, refPath.trim());
branchSHA = fs.readFileSync(branchPath);
} else {
branchSHA = branchName;
}
output.push(branchSHA.slice(0,10));
}
} catch (err) {
console.error(err.stack);
}
return output.join('.');
} else {
return packageVersion;
}
};
});
enifed("container",
["container/container","exports"],
function(__dependency1__, __exports__) {
"use strict";
/*
Public api for the container is still in flux.
The public api, specified on the application namespace should be considered the stable api.
// @module container
@private
*/
/*
Flag to enable/disable model factory injections (disabled by default)
If model factory injections are enabled, models should not be
accessed globally (only through `container.lookupFactory('model:modelName'))`);
*/
Ember.MODEL_FACTORY_INJECTIONS = false;
if (Ember.ENV && typeof Ember.ENV.MODEL_FACTORY_INJECTIONS !== 'undefined') {
Ember.MODEL_FACTORY_INJECTIONS = !!Ember.ENV.MODEL_FACTORY_INJECTIONS;
}
var Container = __dependency1__["default"];
__exports__["default"] = Container;
});
enifed("container/container",
["ember-metal/core","ember-metal/keys","ember-metal/dictionary","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __exports__) {
"use strict";
var Ember = __dependency1__["default"];
// Ember.assert
var emberKeys = __dependency2__["default"];
var dictionary = __dependency3__["default"];
// A lightweight container that helps to assemble and decouple components.
// Public api for the container is still in flux.
// The public api, specified on the application namespace should be considered the stable api.
function Container(parent) {
this.parent = parent;
this.children = [];
this.resolver = parent && parent.resolver || function() {};
this.registry = dictionary(parent ? parent.registry : null);
this.cache = dictionary(parent ? parent.cache : null);
this.factoryCache = dictionary(parent ? parent.factoryCache : null);
this.resolveCache = dictionary(parent ? parent.resolveCache : null);
this.typeInjections = dictionary(parent ? parent.typeInjections : null);
this.injections = dictionary(null);
this.normalizeCache = dictionary(null);
this.validationCache = dictionary(parent ? parent.validationCache : null);
this.factoryTypeInjections = dictionary(parent ? parent.factoryTypeInjections : null);
this.factoryInjections = dictionary(null);
this._options = dictionary(parent ? parent._options : null);
this._typeOptions = dictionary(parent ? parent._typeOptions : null);
}
Container.prototype = {
/**
@property parent
@type Container
@default null
*/
parent: null,
/**
@property children
@type Array
@default []
*/
children: null,
/**
@property resolver
@type function
*/
resolver: null,
/**
@property registry
@type InheritingDict
*/
registry: null,
/**
@property cache
@type InheritingDict
*/
cache: null,
/**
@property typeInjections
@type InheritingDict
*/
typeInjections: null,
/**
@property injections
@type Object
@default {}
*/
injections: null,
/**
@private
@property _options
@type InheritingDict
@default null
*/
_options: null,
/**
@private
@property _typeOptions
@type InheritingDict
*/
_typeOptions: null,
/**
Returns a new child of the current container. These children are configured
to correctly inherit from the current container.
@method child
@return {Container}
*/
child: function() {
var container = new Container(this);
this.children.push(container);
return container;
},
/**
Registers a factory for later injection.
Example:
```javascript
var container = new Container();
container.register('model:user', Person, {singleton: false });
container.register('fruit:favorite', Orange);
container.register('communication:main', Email, {singleton: false});
```
@method register
@param {String} fullName
@param {Function} factory
@param {Object} options
*/
register: function(fullName, factory, options) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
if (factory === undefined) {
throw new TypeError('Attempting to register an unknown factory: `' + fullName + '`');
}
var normalizedName = this.normalize(fullName);
if (normalizedName in this.cache) {
throw new Error('Cannot re-register: `' + fullName +'`, as it has already been looked up.');
}
this.registry[normalizedName] = factory;
this._options[normalizedName] = (options || {});
},
/**
Unregister a fullName
```javascript
var container = new Container();
container.register('model:user', User);
container.lookup('model:user') instanceof User //=> true
container.unregister('model:user')
container.lookup('model:user') === undefined //=> true
```
@method unregister
@param {String} fullName
*/
unregister: function(fullName) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
var normalizedName = this.normalize(fullName);
delete this.registry[normalizedName];
delete this.cache[normalizedName];
delete this.factoryCache[normalizedName];
delete this.resolveCache[normalizedName];
delete this._options[normalizedName];
delete this.validationCache[normalizedName];
},
/**
Given a fullName return the corresponding factory.
By default `resolve` will retrieve the factory from
its container's registry.
```javascript
var container = new Container();
container.register('api:twitter', Twitter);
container.resolve('api:twitter') // => Twitter
```
Optionally the container can be provided with a custom resolver.
If provided, `resolve` will first provide the custom resolver
the opportunity to resolve the fullName, otherwise it will fallback
to the registry.
```javascript
var container = new Container();
container.resolver = function(fullName) {
// lookup via the module system of choice
};
// the twitter factory is added to the module system
container.resolve('api:twitter') // => Twitter
```
@method resolve
@param {String} fullName
@return {Function} fullName's factory
*/
resolve: function(fullName) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
return resolve(this, this.normalize(fullName));
},
/**
A hook that can be used to describe how the resolver will
attempt to find the factory.
For example, the default Ember `.describe` returns the full
class name (including namespace) where Ember's resolver expects
to find the `fullName`.
@method describe
@param {String} fullName
@return {string} described fullName
*/
describe: function(fullName) {
return fullName;
},
/**
A hook to enable custom fullName normalization behaviour
@method normalizeFullName
@param {String} fullName
@return {string} normalized fullName
*/
normalizeFullName: function(fullName) {
return fullName;
},
/**
normalize a fullName based on the applications conventions
@method normalize
@param {String} fullName
@return {string} normalized fullName
*/
normalize: function(fullName) {
return this.normalizeCache[fullName] || (
this.normalizeCache[fullName] = this.normalizeFullName(fullName)
);
},
/**
@method makeToString
@param {any} factory
@param {string} fullName
@return {function} toString function
*/
makeToString: function(factory, fullName) {
return factory.toString();
},
/**
Given a fullName return a corresponding instance.
The default behaviour is for lookup to return a singleton instance.
The singleton is scoped to the container, allowing multiple containers
to all have their own locally scoped singletons.
```javascript
var container = new Container();
container.register('api:twitter', Twitter);
var twitter = container.lookup('api:twitter');
twitter instanceof Twitter; // => true
// by default the container will return singletons
var twitter2 = container.lookup('api:twitter');
twitter2 instanceof Twitter; // => true
twitter === twitter2; //=> true
```
If singletons are not wanted an optional flag can be provided at lookup.
```javascript
var container = new Container();
container.register('api:twitter', Twitter);
var twitter = container.lookup('api:twitter', { singleton: false });
var twitter2 = container.lookup('api:twitter', { singleton: false });
twitter === twitter2; //=> false
```
@method lookup
@param {String} fullName
@param {Object} options
@return {any}
*/
lookup: function(fullName, options) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
return lookup(this, this.normalize(fullName), options);
},
/**
Given a fullName return the corresponding factory.
@method lookupFactory
@param {String} fullName
@return {any}
*/
lookupFactory: function(fullName) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
return factoryFor(this, this.normalize(fullName));
},
/**
Given a fullName check if the container is aware of its factory
or singleton instance.
@method has
@param {String} fullName
@return {Boolean}
*/
has: function(fullName) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
return has(this, this.normalize(fullName));
},
/**
Allow registering options for all factories of a type.
```javascript
var container = new Container();
// if all of type `connection` must not be singletons
container.optionsForType('connection', { singleton: false });
container.register('connection:twitter', TwitterConnection);
container.register('connection:facebook', FacebookConnection);
var twitter = container.lookup('connection:twitter');
var twitter2 = container.lookup('connection:twitter');
twitter === twitter2; // => false
var facebook = container.lookup('connection:facebook');
var facebook2 = container.lookup('connection:facebook');
facebook === facebook2; // => false
```
@method optionsForType
@param {String} type
@param {Object} options
*/
optionsForType: function(type, options) {
if (this.parent) { illegalChildOperation('optionsForType'); }
this._typeOptions[type] = options;
},
/**
@method options
@param {String} fullName
@param {Object} options
*/
options: function(fullName, options) {
options = options || {};
var normalizedName = this.normalize(fullName);
this._options[normalizedName] = options;
},
/**
Used only via `injection`.
Provides a specialized form of injection, specifically enabling
all objects of one type to be injected with a reference to another
object.
For example, provided each object of type `controller` needed a `router`.
one would do the following:
```javascript
var container = new Container();
container.register('router:main', Router);
container.register('controller:user', UserController);
container.register('controller:post', PostController);
container.typeInjection('controller', 'router', 'router:main');
var user = container.lookup('controller:user');
var post = container.lookup('controller:post');
user.router instanceof Router; //=> true
post.router instanceof Router; //=> true
// both controllers share the same router
user.router === post.router; //=> true
```
@private
@method typeInjection
@param {String} type
@param {String} property
@param {String} fullName
*/
typeInjection: function(type, property, fullName) {
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
if (this.parent) { illegalChildOperation('typeInjection'); }
var fullNameType = fullName.split(':')[0];
if (fullNameType === type) {
throw new Error('Cannot inject a `' + fullName +
'` on other ' + type +
'(s). Register the `' + fullName +
'` as a different type and perform the typeInjection.');
}
addTypeInjection(this.typeInjections, type, property, fullName);
},
/**
Defines injection rules.
These rules are used to inject dependencies onto objects when they
are instantiated.
Two forms of injections are possible:
* Injecting one fullName on another fullName
* Injecting one fullName on a type
Example:
```javascript
var container = new Container();
container.register('source:main', Source);
container.register('model:user', User);
container.register('model:post', Post);
// injecting one fullName on another fullName
// eg. each user model gets a post model
container.injection('model:user', 'post', 'model:post');
// injecting one fullName on another type
container.injection('model', 'source', 'source:main');
var user = container.lookup('model:user');
var post = container.lookup('model:post');
user.source instanceof Source; //=> true
post.source instanceof Source; //=> true
user.post instanceof Post; //=> true
// and both models share the same source
user.source === post.source; //=> true
```
@method injection
@param {String} factoryName
@param {String} property
@param {String} injectionName
*/
injection: function(fullName, property, injectionName) {
if (this.parent) { illegalChildOperation('injection'); }
validateFullName(injectionName);
var normalizedInjectionName = this.normalize(injectionName);
if (fullName.indexOf(':') === -1) {
return this.typeInjection(fullName, property, normalizedInjectionName);
}
Ember.assert('fullName must be a proper full name', validateFullName(fullName));
var normalizedName = this.normalize(fullName);
if (this.cache[normalizedName]) {
throw new Error("Attempted to register an injection for a type that has already been looked up. ('" +
normalizedName + "', '" +
property + "', '" +
injectionName + "')");
}
addInjection(initRules(this.injections, normalizedName), property, normalizedInjectionName);
},
/**
Used only via `factoryInjection`.
Provides a specialized form of injection, specifically enabling
all factory of one type to be injected with a reference to another
object.
For example, provided each factory of type `model` needed a `store`.
one would do the following:
```javascript
var container = new Container();
container.register('store:main', SomeStore);
container.factoryTypeInjection('model', 'store', 'store:main');
var store = container.lookup('store:main');
var UserFactory = container.lookupFactory('model:user');
UserFactory.store instanceof SomeStore; //=> true
```
@private
@method factoryTypeInjection
@param {String} type
@param {String} property
@param {String} fullName
*/
factoryTypeInjection: function(type, property, fullName) {
if (this.parent) { illegalChildOperation('factoryTypeInjection'); }
addTypeInjection(this.factoryTypeInjections, type, property, this.normalize(fullName));
},
/**
Defines factory injection rules.
Similar to regular injection rules, but are run against factories, via
`Container#lookupFactory`.
These rules are used to inject objects onto factories when they
are looked up.
Two forms of injections are possible:
* Injecting one fullName on another fullName
* Injecting one fullName on a type
Example:
```javascript
var container = new Container();
container.register('store:main', Store);
container.register('store:secondary', OtherStore);
container.register('model:user', User);
container.register('model:post', Post);
// injecting one fullName on another type
container.factoryInjection('model', 'store', 'store:main');
// injecting one fullName on another fullName
container.factoryInjection('model:post', 'secondaryStore', 'store:secondary');
var UserFactory = container.lookupFactory('model:user');
var PostFactory = container.lookupFactory('model:post');
var store = container.lookup('store:main');
UserFactory.store instanceof Store; //=> true
UserFactory.secondaryStore instanceof OtherStore; //=> false
PostFactory.store instanceof Store; //=> true
PostFactory.secondaryStore instanceof OtherStore; //=> true
// and both models share the same source instance
UserFactory.store === PostFactory.store; //=> true
```
@method factoryInjection
@param {String} factoryName
@param {String} property
@param {String} injectionName
*/
factoryInjection: function(fullName, property, injectionName) {
if (this.parent) { illegalChildOperation('injection'); }
var normalizedName = this.normalize(fullName);
var normalizedInjectionName = this.nor