Skip to content

Commit

Permalink
Change: Module constructors receive dependencies
Browse files Browse the repository at this point in the history
The module was just passing a single argument: score itself. Changed
that behaviour for the new module loading feature to work: If a module
is registered with dependencies X and Y, it will receive those
dependencies as arguments to its factory:

    score.extend('foo', ['X', 'Y'], function(X, Y) {
        return 'value-of-foo';
    });
  • Loading branch information
soulmerge committed Mar 6, 2017
1 parent fbdda6d commit d3ee913
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions init.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
__version__: '0.0.3',

extend: function(fullName, dependencies, callback) {
var i, j, tmp, nextName, parts, currentPart, nextPart, missing = [];
var i, j, tmp, nextName, parts, currentPart, nextPart, args = [], missing = [];
for (i = 0; i < dependencies.length; i++) {
parts = dependencies[i].split('.');
currentPart = score;
Expand All @@ -77,6 +77,7 @@
}
currentPart = nextPart;
}
args.push(currentPart);
}
currentPart = score;
parts = fullName.split('.');
Expand Down Expand Up @@ -109,7 +110,7 @@
};
Object.defineProperties(currentPart, tmp);
} else {
currentPart[nextName] = callback.call(score, score);
currentPart[nextName] = callback.apply(score, args);
tmp = __queue;
__queue = [];
for (i = 0; i < tmp.length; i++) {
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('score', function() {
it('should assign return value to score', function(done) {
loadScore(function(score) {
expect(score.foo).to.be(undefined);
score.extend('foo', [], function(score2) {
expect(score).to.equal(score2);
score.extend('foo', [], function() {
expect(this).to.equal(score);
return 81;
});
expect(score.foo).to.equal(81);
Expand Down Expand Up @@ -58,10 +58,10 @@ describe('score', function() {
expect(score.foo).to.be(undefined);
var barLoaded = false;
var fooBarLoaded = false;
score.extend('foo', ['bar'], function(score) {
score.extend('foo', ['bar'], function(bar) {
expect(barLoaded).to.be(true);
expect(fooBarLoaded).to.be(false);
expect(score.bar).to.be('frobination');
expect(bar).to.be('frobination');
fooBarLoaded = true;
return 81;
});
Expand Down

0 comments on commit d3ee913

Please sign in to comment.