Skip to content

Commit

Permalink
Calendar options binding/triggering system
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Jul 10, 2016
1 parent b9e35ed commit a2ed02c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions lumbar.json
Expand Up @@ -38,6 +38,7 @@
"src/common/View.js",
"src/common/Scroller.js",
"src/Calendar.js",
"src/Calendar.options.js",
"src/defaults.js",
"src/lang.js",
"src/Header.js",
Expand Down
62 changes: 62 additions & 0 deletions src/Calendar.options.js
@@ -0,0 +1,62 @@
/*
Options binding/triggering system.
*/
Calendar.mixin({

// A map of option names to arrays of handler objects. Initialized to {} in Calendar.
// Format for a handler object:
// {
// func // callback function to be called upon change
// names // option names whose values should be given to func
// }
optionHandlers: null,

// Calls handlerFunc immediately, and when the given option has changed.
// handlerFunc will be given the option value.
bindOption: function(optionName, handlerFunc) {
this.bindOptions([ optionName ], handlerFunc);
},

// Calls handlerFunc immediately, and when any of the given options change.
// handlerFunc will be given each option value as ordered function arguments.
bindOptions: function(optionNames, handlerFunc) {
var handlerObj = { func: handlerFunc, names: optionNames };
var i;

for (i = 0; i < optionNames.length; i++) {
this.registerOptionHandlerObj(optionNames[i], handlerObj);
}

this.triggerOptionHandlerObj(handlerObj);
},

// Puts the given handler object into the internal hash
registerOptionHandlerObj: function(optionName, handlerObj) {
(this.optionHandlers[optionName] || (this.optionHandlers[optionName] = []))
.push(handlerObj);
},

// Reports that the given option has changed, and calls all appropriate handlers.
triggerOptionHandlers: function(optionName) {
var handlerObjs = this.optionHandlers[optionName] || [];
var i;

for (i = 0; i < handlerObjs.length; i++) {
this.triggerOptionHandlerObj(handlerObjs[i]);
}
},

// Calls the callback for a specific handler object, passing in the appropriate arguments.
triggerOptionHandlerObj: function(handlerObj) {
var optionNames = handlerObj.names;
var optionValues = [];
var i;

for (i = 0; i < optionNames.length; i++) {
optionValues.push(this.options[optionNames[i]]);
}

handlerObj.func.apply(this, optionValues); // maintain the Calendar's `this` context
}

});

0 comments on commit a2ed02c

Please sign in to comment.