mirror of
https://github.com/Hopiu/postal.js.git
synced 2026-03-16 22:20:23 +00:00
Ensuring context set for delayed callbacks
- also, fixing standalone test runner
This commit is contained in:
parent
ae5548cc3f
commit
dfde7b48d6
3 changed files with 31 additions and 7 deletions
|
|
@ -119,6 +119,17 @@ describe( "SubscriptionDefinition", function () {
|
|||
sDefe.callback( "second", { topic : "TestTopic" } );
|
||||
results.push( "first" );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key: 1234
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).defer();
|
||||
sDefe.callback( "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( "When delaying the callback", function () {
|
||||
|
|
@ -135,6 +146,17 @@ describe( "SubscriptionDefinition", function () {
|
|||
sDefe.callback( "second", { topic : "TestTopic" } );
|
||||
results.push( "first" );
|
||||
} );
|
||||
|
||||
it( "Should keep the context intact", function ( done ) {
|
||||
var context = {
|
||||
key: 1234
|
||||
};
|
||||
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
|
||||
expect( this ).to.be( context );
|
||||
done();
|
||||
} ).withContext(context).withDelay( 200 );
|
||||
sDefe.callback( "stuff", { topic : "TestTopic" } );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( "When debouncing the callback", function () {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/ext/mocha.css" />
|
||||
<link rel="stylesheet" href="../ext/mocha.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
<script type="text/javascript" src="/ext/jquery-1.7.1.js"></script>
|
||||
<script type="text/javascript" src="/ext/underscore.js"></script>
|
||||
<script src="/ext/expect.js"></script>
|
||||
<script src="/ext/mocha.js"></script>
|
||||
<script type="text/javascript" src="../ext/jquery-1.7.1.js"></script>
|
||||
<script type="text/javascript" src="../ext/underscore.js"></script>
|
||||
<script type="text/javascript" src="../ext/expect.js"></script>
|
||||
<script type="text/javascript" src="../ext/mocha.js"></script>
|
||||
<script type="text/javascript">
|
||||
mocha.setup({ ui: 'bdd', timeout: 60000, ignoreLeaks: true });
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -37,9 +37,10 @@ SubscriptionDefinition.prototype = {
|
|||
|
||||
defer : function () {
|
||||
var fn = this.callback;
|
||||
var context = this.context;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn( data, env );
|
||||
fn.call( context, data, env );
|
||||
}, 0 );
|
||||
};
|
||||
return this;
|
||||
|
|
@ -113,9 +114,10 @@ SubscriptionDefinition.prototype = {
|
|||
throw "Milliseconds must be a number";
|
||||
}
|
||||
var fn = this.callback;
|
||||
var context = this.context;
|
||||
this.callback = function ( data, env ) {
|
||||
setTimeout( function () {
|
||||
fn( data, env );
|
||||
fn.call( context, data, env );
|
||||
}, milliseconds );
|
||||
};
|
||||
return this;
|
||||
|
|
|
|||
Loading…
Reference in a new issue