Path: blob/master/webroot/rsrc/externals/javelin/lib/behavior.js
12242 views
/**1* @provides javelin-behavior2* @requires javelin-magical-init3* javelin-util4*5* @javelin-installs JX.behavior6* @javelin-installs JX.initBehaviors7*8* @javelin9*/1011/**12* Define a Javelin behavior, which holds glue code in a structured way. See13* @{article:Concepts: Behaviors} for a detailed description of Javelin14* behaviors.15*16* To define a behavior, provide a name and a function:17*18* JX.behavior('win-a-hog', function(config, statics) {19* alert("YOU WON A HOG NAMED " + config.hogName + "!");20* });21*22* @param string Behavior name.23* @param function Behavior callback/definition.24* @return void25*/26JX.behavior = function(name, control_function) {27if (__DEV__) {28if (JX.behavior._behaviors.hasOwnProperty(name)) {29JX.$E(30'JX.behavior("' + name + '", ...): '+31'behavior is already registered.');32}33if (!control_function) {34JX.$E(35'JX.behavior("' + name + '", <nothing>): '+36'initialization function is required.');37}38if (typeof control_function != 'function') {39JX.$E(40'JX.behavior("' + name + '", <garbage>): ' +41'initialization function is not a function.');42}43// IE does not enumerate over these properties44var enumerables = {45toString: true,46hasOwnProperty: true,47valueOf: true,48isPrototypeOf: true,49propertyIsEnumerable: true,50toLocaleString: true,51constructor: true52};53if (enumerables[name]) {54JX.$E(55'JX.behavior("' + name + '", <garbage>): ' +56'do not use this property as a behavior.'57);58}59}60JX.behavior._behaviors[name] = control_function;61JX.behavior._statics[name] = {};62};636465/**66* Execute previously defined Javelin behaviors, running the glue code they67* contain to glue stuff together. See @{article:Concepts: Behaviors} for more68* information on Javelin behaviors.69*70* Normally, you do not call this function yourself; instead, your server-side71* library builds it for you.72*73* @param dict Map of behaviors to invoke: keys are behavior names, and values74* are lists of configuration dictionaries. The behavior will be75* invoked once for each configuration dictionary.76* @return void77*/78JX.initBehaviors = function(map) {79var missing_behaviors = [];80for (var name in map) {81if (!(name in JX.behavior._behaviors)) {82missing_behaviors.push(name);83continue;84}85var configs = map[name];86if (!configs.length) {87if (JX.behavior._initialized.hasOwnProperty(name)) {88continue;89}90configs = [null];91}92for (var ii = 0; ii < configs.length; ii++) {93try {94JX.behavior._behaviors[name](configs[ii], JX.behavior._statics[name]);95} catch (behavior_exception) {96JX.log(97'JX.initBehaviors(...): behavior "%s" raised an error during setup.',98name);99JX.log(behavior_exception);100}101}102JX.behavior._initialized[name] = true;103}104if (missing_behaviors.length) {105JX.$E(106'JX.initBehavior(map): behavior(s) not registered: ' +107missing_behaviors.join(', ')108);109}110};111112JX.behavior._behaviors = {};113JX.behavior._statics = {};114JX.behavior._initialized = {};115JX.flushHoldingQueue('behavior', JX.behavior);116117118