Created
February 25, 2012 06:22
-
-
Save jfhbrook/1906886 to your computer and use it in GitHub Desktop.
THE FRAMEWORK YOUR FRAMEWORK COULD CODE LIKE
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
var flatiron = require('flatiron'), | |
app = flatiron.app; | |
console.log('What\'s that in your hand? Look down,'); | |
app.use(require('./tickets')); | |
console.log('look up. I HAVE IT. It\'s ' + app.qty + ' TICKETS to ' + app.description + '!'); | |
console.log('Look again.'); | |
app.use(require('./diamonds')); | |
console.log('THE TICKETS ARE NOW DIAMONDS'); | |
app.sparkle(); | |
setTimeout(function () { | |
app.stopSparkling(); | |
}, 6000); | |
app.on('sparkling::done', function () { | |
console.log('Anything is possible when you use flatiron and stop using express.'); | |
console.log('I\'m on a horse.'); | |
}); |
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
exports.attach = function () { | |
var sparkling = false; | |
var that = this; | |
this.sparkle = function sparkle () { | |
sparkling = true; | |
setTimeout(function () { | |
console.log('* bling *'); | |
if (sparkling) { | |
sparkle(); | |
} | |
else { | |
that.emit('sparkling::done'); | |
} | |
}, 2000 * Math.random()); | |
}; | |
this.stopSparkling = function () { | |
sparkling = false; | |
}; | |
} |
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
22:35 josh@onix /tmp/old-spice | |
% node app.js | |
What's that in your hand? Look down, | |
look up. I HAVE IT. It's 2 TICKETS to that thing you love! | |
Look again. | |
THE TICKETS ARE NOW DIAMONDS | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
* bling * | |
Anything is possible when you use flatiron and stop using express. | |
I'm on a horse. | |
22:35 josh@onix /tmp/old-spice | |
% |
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
exports.attach = function (opts) { | |
opts = opts || {}; | |
var that = this; | |
this.qty = opts.qty || 2; | |
this.description = opts.description || 'that thing you love'; | |
this.redeem = function () { | |
console.log('You go to see ' + that.description + ' and it\'s SPECTACULAR.'); | |
that.qty--; | |
}; | |
} |
devil's advocatingly, with express you could do something like:
var diamonds = require('./diamonds.js');
app.use(diamonds);
diamonds.sparkle();
setTimeout(function () { diamonds.stopSparkling() }, 6000);
I'm not sure how applicable that approach would be to flatiron but it seems like a better way of organizing functionality since everything isn't glued onto the app
object.
How would this work though? I mean, what's the point of doing an express app.use if you're not handling any http requests? Flatiron stock is basically just an IoC container with some utility functions strapped on unless you app.use(flatiron.plugins.http)
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good idea! I'm still using a setTimeout, but now I'm using an event so that sparkling can finish properly.