forked from cello/twine
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added factory fiber - useful for dynamically resolving components
- Loading branch information
1 parent
2dd7406
commit 141c4d0
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @license Copyright (c) 2011 Cello Software, LLC. | ||
* All rights reserved. | ||
* Available via the new BSD License. | ||
*/ | ||
/*jshint | ||
bitwise: false, curly: true, eqeqeq: true, forin: true, immed: true, indent: 4, maxlen: 100, | ||
newcap: true, noarg: true, noempty: true, onevar: true, passfail: false, strict: true, | ||
undef: true, white: true | ||
*/ | ||
/*global define: false, require: false */ | ||
|
||
define([ | ||
'compose', | ||
'lang', | ||
'promise', | ||
'dojo/_base/array' | ||
], function (compose, lang, promise, array) { | ||
'use strict'; | ||
return compose(function FactoryFiber() { | ||
this._listeners = []; | ||
}, { | ||
id: 'Factory Fiber', | ||
|
||
init: function (kernel) { | ||
this.kernel = kernel; | ||
kernel.modelBuilder.addProcessor(this); | ||
}, | ||
|
||
process: function (model, next) { | ||
var kernel = this.kernel; | ||
return promise.when(model, function (model) { | ||
var factory = model.factory, | ||
module = {}; | ||
|
||
if (factory) { | ||
// model.factory is a map of | ||
// propertyName -> moduleId | ||
// instance.propertyName will be a factory for a moduleId | ||
array.forEach(lang.keys(factory), function (property) { | ||
module[property] = function (args) { | ||
return kernel.resolve(factory[property], args); | ||
}; | ||
}); | ||
model.module = module; | ||
} | ||
}); | ||
}, | ||
|
||
terminate: function () { | ||
// stop all the listeners | ||
array.forEach(this._listeners, function (listener) { | ||
listener.remove(); | ||
}); | ||
} | ||
}); | ||
}); |