Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calendar options binding/triggering system
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
}); |