Last active
December 22, 2015 06:38
-
-
Save jcblw/6432263 to your computer and use it in GitHub Desktop.
reply functions on event emitters jacoblwe20/marrow
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
// when attaching to events of a seperate objects, or constructors | |
// attach a reply function to the end of arguments passes to the handle function | |
foo.on( bar, 'qux', function ( payload, reply ) { | |
// i see your payload... | |
if ( payload ) { | |
reply( 'Looks good' ); | |
} | |
}); | |
bar.emit( 'qux', true, function ( payload ) { | |
console.log( payload ); | |
// 'Looks good' | |
}); | |
// this currently works!, | |
// but unless a anon function passed in it can throw an error | |
// the goal is to remove that worry and maybe | |
// attach some emiters to the replies | |
// this could also add some cool behaviors | |
// to the relationship between components | |
qux.on( bar, 'foo', function ( amount, reply ) { | |
for( var i = 0; i < amount; i ++ ){ | |
reply( Math.random() * 10000 ); | |
} | |
}); | |
bar.emit( 'foo', 3, function ( random ) { | |
// random will be three differnt random numbers | |
}); | |
// a more real world example | |
DS.on( app, 'get:users', function( ids, reply ){ | |
// access private variable pull info | |
// maybe even fetch | |
// send back a payload | |
}); | |
// validation component | |
app.on( form, 'validate', function ( payload, reply ) { | |
// do validation | |
reply({ isValid: true }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment