0.1.5 • Published 9 years ago
toggle.js v0.1.5
toggle.js
A library for all things toggly
Install
npm install toggle.js
Use in node
var toggle = require('toggle.js');
/* ... */
Use in web page
<script src="./node_modules/toggle.js/dist/toggle.min.js"></script>
togg
Calling toggle returns a togg which is a function that when called internally iterates to the next thing in your toggled list
Example
var togg = toggle('Eric', 'Stan', 'Kyle', 'Kenny');
togg(); // Eric
togg(); // Stan
togg(); // Kyle
togg(); // Kenny
togg(); // Eric
/* ... */
As you can see we perform an inorder iteration of the parameters to toggle.
Event handlers
toggle is great for event handlers
$('#btn').click(toggle(function () {
console.log('odd number of clicks');
}, function () {
console.log('even number of clicks');
}));
Mix and match
You can have a mix of values and functions.
var togg = toggle('Sunday', 'Monday', function () {return 'Tuesday';}, 'Wednesday',
'Thursday', 'Friday', function () {return 'Saturday'});
togg(); // Sunday
togg(); // Monday
togg(); // Tuesday
togg(); // Wednesday
togg(); // Thursday
togg(); // Friday
togg(); // Saturday
togg(); // Sunday
Parameter Forwarding
You can forward parameters from toggs to the underlying function.
var dbs = [
{
name: 'Cool_guy.db',
conn: '10.0.0.23'
},
{
name: 'Not_as_cool_guy.db',
conn: '10.2.10.02'
},
{
name: 'Lame_guy.db',
conn: '10.0.0.1'
}
];
function query (db) {
console.log('Dropping ' + db.name);
}
var togg = toggle(function (db) {
console.log('Setting up connection to: ' + db.conn);
}, function (db) {
console.log('Tearing down connection to: ' + db.conn);
});
dbs.forEach(function (db) {
togg(db); // Setting up connection to: 10.0.0.23
query(db); // Dropping Cool_guy.db
togg(db); // Tearing down connection to: 10.0.0.23
});