Skip to content

Commit

Permalink
ES3 downgrade.
Browse files Browse the repository at this point in the history
  • Loading branch information
benquarmby committed Jan 28, 2015
1 parent 0d9a324 commit 78a6f40
Showing 1 changed file with 65 additions and 50 deletions.
115 changes: 65 additions & 50 deletions source/kontainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
/*!
Kontainer 0.1.0
Copyright © Ben Quarmby 2015
https://github.com/benquarmby/kontainer/
Expand All @@ -21,25 +21,26 @@ kontainer = (function () {
mockContainer;

function validateFactory(factory) {
if (!Array.isArray(factory)) {
if (!(factory instanceof Array)) {
throw new Error('Factories must always be arrays.');
}

var last = factory.length - 1;
var i,
len = factory.length,
last = len - 1,
item;

factory.forEach(function (item, index) {
if (index === last) {
for (i = 0; i < len; i += 1) {
item = factory[i];

if (i === last) {
if (typeof item !== 'function') {
throw new Error('The last element in a factory array must be a function.');
}

return;
}

if (typeof item !== 'string') {
} else if (typeof item !== 'string') {
throw new Error('Each element in a factory array before the function must be a string.');
}
});
}
}

function Container() {
Expand Down Expand Up @@ -77,11 +78,18 @@ kontainer = (function () {
inject: function (factory, path, custom) {
var self = this,
fn = factory.pop(),
args;
args = [],
i,
len,
name,
value;

args = factory.map(function (key) {
return custom && custom.hasOwnProperty(key) ? custom[key] : self.resolve(key, path);
});
for (i = 0, len = factory.length; i < len; i += 1) {
name = factory[i];
value = custom && custom.hasOwnProperty(name) ? custom[name] : self.resolve(name, path);

args.push(value);
}

return fn.apply(undefined, args);
},
Expand Down Expand Up @@ -111,7 +119,7 @@ kontainer = (function () {
},

register: function (name, value) {
if (Array.isArray(value)) {
if (value instanceof Array) {
this.registerFactory(name, value);

return;
Expand All @@ -122,49 +130,28 @@ kontainer = (function () {
};

container = new Container();

function createViewModel(factory, name, params, componentInfo) {
return container.inject(factory, [name], {
params: params,
componentInfo: componentInfo
});
}

function loadViewModel(componentName, viewModelConfig, callback) {
if (Array.isArray(viewModelConfig)) {
validateFactory(viewModelConfig);
callback(createViewModel.bind(null, viewModelConfig, componentName));

return;
}

callback(null);
}

mockContainer = new Container();

function mockInject(factory, custom) {
validateFactory(factory);

return mockContainer.inject(factory, [], custom);
}

return {
/**
* Registers a factory with the container.
* @method
* @param {String} name The name of the dependency.
* @param {Array} factory The factory array.
*/
registerFactory: container.registerFactory.bind(container),
registerFactory: function (name, factory) {
container.registerFactory(name, factory);
},

/**
* Registers a value with the container.
* @method
* @param {String} name The name of the dependency.
* @param {Object} value The value.
*/
registerValue: container.registerValue.bind(container),
registerValue: function (name, value) {
container.registerValue(name, value);
},

/**
* Registers a dependency with the container.
Expand All @@ -174,14 +161,31 @@ kontainer = (function () {
* @param {String} name The name of the dependency.
* @param {Object} value The factory array or value.
*/
register: container.register.bind(container),
register: function (name, value) {
container.register(name, value);
},

/**
* The component loader to be registered with Knockout.
* ko.components.loaders.unshift(kontainer.loader)
*/
loader: {
loadViewModel: loadViewModel
loadViewModel: function (componentName, viewModelConfig, callback) {
if (!(viewModelConfig instanceof Array)) {
callback(null);

return;
}

validateFactory(viewModelConfig);

callback(function (params, componentInfo) {
return container.inject(viewModelConfig, [componentName], {
params: params,
componentInfo: componentInfo
});
});
}
},

mock: {
Expand All @@ -191,15 +195,19 @@ kontainer = (function () {
* @param {String} name The name of the dependency.
* @param {Array} factory The factory array.
*/
registerFactory: mockContainer.registerFactory.bind(mockContainer),
registerFactory: function (name, factory) {
mockContainer.registerFactory(name, factory);
},

/**
* Registers a value with the mock container.
* @method
* @param {String} name The name of the dependency.
* @param {Object} value The value.
*/
registerValue: mockContainer.registerValue.bind(mockContainer),
registerValue: function (name, value) {
mockContainer.registerValue(name, value);
},

/**
* Registers a dependency with the mock container.
Expand All @@ -209,16 +217,23 @@ kontainer = (function () {
* @param {String} name The name of the dependency.
* @param {Object} value The factory array or value.
*/
register: mockContainer.register.bind(mockContainer),
register: function (name, value) {
mockContainer.register(name, value);
},

/**
* Resolves a factory, injecting it with dependencies
* from the mock container.
* from the mock container or specified custom values.
* @method
* @param {Array} factory The factory array.
* @param {Object} custom A dictionary of custom values to inject.
* @returns {Object} The product of the factory.
*/
inject: mockInject
inject: function (factory, custom) {
validateFactory(factory);

return mockContainer.inject(factory, [], custom);
}
}
};
}());

0 comments on commit 78a6f40

Please sign in to comment.