Skip to content

Commit

Permalink
Add in the concept of wrapped vs. unwrapped scripts. default to unwra…
Browse files Browse the repository at this point in the history
…pped in injectJs, but will default to wrapped in normal yepnope mode.
  • Loading branch information
SlexAxton committed Mar 18, 2013
1 parent efc5f0a commit ca71d8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/yepnope.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ window.yepnope = (function (window, document, undef) {
}
cb();
}
else if (justTheCallback) {
cb();
}
}

// Inject a script into the page and know when it's done
Expand Down Expand Up @@ -122,6 +125,11 @@ window.yepnope = (function (window, document, undef) {

script.src = src;

// Ensure that his url is marked as active,
// so we don't try to load it with itself
// simultaneously.
scriptCache[src] = true;

// IE Race condition
// http://jaubourg.net/2010/07/loading-script-as-onclick-handler-of.html
if (isOldIE) {
Expand Down Expand Up @@ -152,8 +160,13 @@ window.yepnope = (function (window, document, undef) {
catch (e) {}
}

scriptCache[src] = scriptsQueue.shift();
runWhenReady(src, cb);
if (wrapped) {
scriptCache[src] = scriptsQueue.shift();
}

// Run the inside and then the callback if it's wrapped,
// otherwise, just run the callback
runWhenReady(src, cb, !wrapped);
}

// Handle memory leak in IE
Expand Down
21 changes: 15 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ describe('yepnope', function() {
yepnope.injectJs(s.url, function () {
expect(yeptest).to.have.property(s.name);
done();
});
}, null, null, true);
});

it('should allow me to inject a script without a wrapper', function (done) {
var s = js(null, true);

yepnope.injectJs(s.url, function () {
expect(yeptest).to.have.property(s.name);
done();
}, null, null, null);
});

it('should be tolerant of long loading scripts', function (done) {
Expand All @@ -50,14 +59,14 @@ describe('yepnope', function() {
yepnope.injectJs(s.url, function () {
expect(yeptest).to.have.property(s.name);
done();
});
}, null, null, true);
});

it('should throw an error on a 404', function (done) {
yepnope.injectJs('/s/NOTFOUND.js', function (err) {
expect(err).to.be.instanceOf(Error);
done();
});
}, null, null, true);
});

it('should timeout if it takes too long, and be cancelled', function (done) {
Expand All @@ -80,7 +89,7 @@ describe('yepnope', function() {
yepnope.errorTimeout = oldTimeout;
done();
}, 400);
});
}, null, null, true);
});

it('should listen to the local timeout over the global timeout', function (done) {
Expand All @@ -93,7 +102,7 @@ describe('yepnope', function() {
expect(yeptest).to.not.have.property(s.name);
done();
}, 400);
}, null, 100);
}, null, 100, true);
// The fourth argument is a local timeout
});

Expand All @@ -111,7 +120,7 @@ describe('yepnope', function() {
}, {
'id' : id,
'name': id
});
}, null, true);
});

});
Expand Down

0 comments on commit ca71d8c

Please sign in to comment.