551 lines
14 KiB
HTML
Executable File
551 lines
14 KiB
HTML
Executable File
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>DocStrap Source: mixins/signalable.js</title>
|
|
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/site.spruce.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
<div class="container-fluid">
|
|
<div class="navbar navbar-fixed-top navbar-inverse">
|
|
<div class="navbar-inner">
|
|
<a class="brand" href="index.html">DocStrap</a>
|
|
<ul class="nav">
|
|
|
|
<li class="dropdown">
|
|
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li>
|
|
<a href="module-base.html">base</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="chains_.html">base/chains</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="binder.html">documents/binder</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="model_.html">documents/model</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="probe.html">documents/probe</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="schema_.html">documents/schema</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="collector.html">ink/collector</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="bussable_.html">mixins/bussable</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="signalable_.html">mixins/signalable</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="format.html">strings/format</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="logger.html">utils/logger</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li>
|
|
<a href="base.html">base</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="chains.html">base/chains</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="model.html">documents/model</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="probe.queryOperators.html">documents/probe.queryOperators</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="probe.updateOperators.html">documents/probe.updateOperators</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="collector-ACollector.html">ink/collector~ACollector</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="collector-CollectorBase_.html">ink/collector~CollectorBase</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="collector-OCollector.html">ink/collector~OCollector</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="signalable-Signal.html">mixins/signalable~Signal</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="logger.Logger.html">utils/logger.Logger</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="mixins.list.html" class="dropdown-toggle" data-toggle="dropdown">Mixins<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li>
|
|
<a href="schema.html">documents/schema</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="bussable.html">mixins/bussable</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="signalable.html">mixins/signalable</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="tutorials.list.html" class="dropdown-toggle" data-toggle="dropdown">Tutorials<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li>
|
|
<a href="tutorial-Teeth.html">Brush Teeth</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="tutorial-Car.html">Drive Car</a>
|
|
</li>
|
|
|
|
<li>
|
|
<a href="tutorial-Test.html">Fence Test</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li>
|
|
<a href="global.html#utils/logger">utils/logger</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row-fluid">
|
|
|
|
|
|
<div class="span12">
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: mixins/signalable.js</h1>
|
|
|
|
<section>
|
|
<article>
|
|
<pre
|
|
class="sunlight-highlight-javascript linenums">"use strict";
|
|
/**
|
|
* @fileOverview Add the ability to fire signals on your objects. Signals are events, but hard coded into the object
|
|
* and don't rely on strings like other events and `eventables`
|
|
* @module mixins/signalable
|
|
* @requires base
|
|
* @requires signals
|
|
* @requires base/logger
|
|
*/
|
|
|
|
var Base = require( "../base" );
|
|
var signals = require( 'signals' );
|
|
var format = require( "../strings/format" );
|
|
var sys = require( "lodash" );
|
|
|
|
/**
|
|
* @typedef
|
|
* @property {boolean=} memorize If Signal should keep record of previously dispatched parameters and automatically execute listener. Defaults to `false`
|
|
* @property {array=} params Default parameters passed to listener during `Signal.raise`/`Signal.fire`/`Signal.trigger` and SignalBinding.execute. (curried parameters). Defaults to `null`
|
|
* @property {object=} context When provided the signal will be raised in the context of this object. Defaults to `this` - the signal host
|
|
* @name SignalOptions
|
|
* @memberOf module:mixins/signalable
|
|
* @example
|
|
*
|
|
* signals:{
|
|
* opened: null,
|
|
* twisted: { memorize:true },
|
|
* applied: { memorize: false, params:[one, two] }
|
|
* }
|
|
*
|
|
* // Setting the context initially can be a hassle, so this also supports a function that returns a hash
|
|
*
|
|
* signals: function(){
|
|
* return {
|
|
* opened: null,
|
|
* twisted: { memorize:true },
|
|
* applied: { memorize: false, params:[one, two] },
|
|
* reversed: {context: someOtherRuntimeObject}
|
|
* };
|
|
* }
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* @classDesc A signal that can be raised on an object. When you deploy the `Signalable` mixin, it
|
|
* creates instances of these for you.
|
|
*
|
|
* @constructor
|
|
* @param {?object} host If hosted, you can identify the host here.
|
|
* @param {?string} name The name of the signal
|
|
* @type module:mixins/signalable.SignalOptions
|
|
*/
|
|
var Signal = Base.compose( [Base, signals.Signal], /** @lends module:mixins/signalable~Signal# */{
|
|
declaredClass : "mixins/Signal",
|
|
|
|
constructor : function ( host, name, options ) {
|
|
options = options || {};
|
|
this.memorize = options.memorize === true;
|
|
this.host = host;
|
|
this.trigger = this.fire = this.raise = this.dispatch;
|
|
this.name = name || sys.uniqueId( "signal" );
|
|
this.params = options.params;
|
|
this.defaultContext = options.context;
|
|
},
|
|
|
|
/**
|
|
* Cleans up
|
|
* @private
|
|
*/
|
|
destroy : function () {
|
|
this.removeAll();
|
|
this.dispose();
|
|
this.host = null;
|
|
},
|
|
|
|
/**
|
|
* Ties a listener to a signal.
|
|
* @param {function} listener The function to call when the signal is raised
|
|
* @param {?object} listenerContext A context to set for the listener. The event host may set a default for this value, but you may override that here.
|
|
* @param {?number} priority A priority for the listener.
|
|
* @returns {SignalBinding}
|
|
*/
|
|
on : function ( listener, listenerContext, priority ) {
|
|
if ( sys.isNumber( listenerContext ) ) {
|
|
priority = listenerContext;
|
|
listenerContext = null;
|
|
}
|
|
listenerContext = listenerContext || this.defaultContext || this.host;
|
|
var binding = this.add( listener, listenerContext, priority );
|
|
if ( this.options.params ) {
|
|
binding.params = this.arams;
|
|
}
|
|
return binding;
|
|
},
|
|
/**
|
|
* Ties a listener to for a signal for one execution.
|
|
* @param {function} listener The function to call when the signal is raised
|
|
* @param {?object} listenerContext A context to set for the listener. The event host may set a default for this value, but you may override that here.
|
|
* @param {?number} priority A priority for the listener.
|
|
* @returns {SignalBinding}
|
|
*/
|
|
once : function ( listener, listenerContext, priority ) {
|
|
if ( sys.isNumber( listenerContext ) ) {
|
|
priority = listenerContext;
|
|
listenerContext = null;
|
|
}
|
|
listenerContext = listenerContext || this.defaultContext || this.host;
|
|
var binding = this.addOnce( listener, listenerContext, priority );
|
|
if ( this.options.params ) {
|
|
binding.params = this.params;
|
|
}
|
|
return binding;
|
|
},
|
|
/**
|
|
* Unbinds a listener to a signal.
|
|
* @param {function} listener The function to unbind
|
|
* @param {?object} listenerContext The context that was bound
|
|
* @returns {function}
|
|
*/
|
|
off : function ( listener, listenerContext ) {
|
|
listenerContext = listenerContext || this.host;
|
|
return this.remove( listener, listenerContext );
|
|
},
|
|
/**
|
|
* Check if listener was attached to Signal.
|
|
* @param {function} listener The function to check
|
|
* @param {?object} listenerContext The context that was bound
|
|
* @returns {boolean}
|
|
*/
|
|
has : function ( listener, listenerContext ) {
|
|
listenerContext = listenerContext || this.defaultContext || this.host;
|
|
return this.remove( listener, listenerContext );
|
|
},
|
|
/**
|
|
* Strings!
|
|
*/
|
|
toString : function () {
|
|
return format( "{0}\nname:{1}\nlisteners:{2}",
|
|
this.declaredClass,
|
|
this.name,
|
|
this.getNumListeners()
|
|
);
|
|
}
|
|
|
|
} );
|
|
|
|
/**
|
|
* @classDesc Make an object capable of handling a signal. Or many signals.
|
|
* @exports mixins/signalable
|
|
* @mixin
|
|
* @extends base
|
|
*/
|
|
var Signalable = Base.compose( [Base], /** @lends mixins/signalable# */{
|
|
declaredClass : "mixins/Signalable",
|
|
|
|
constructor : function () {
|
|
this.autoLoadSignals = this.autoLoadSignals || true;
|
|
if ( this.autoLoadSignals === true ) {
|
|
this._loadSignals();
|
|
}
|
|
},
|
|
/**
|
|
* When you make a change to the signals hash after loading, then you can make it reload
|
|
*/
|
|
refreshSignals : function () {
|
|
this._loadSignals();
|
|
},
|
|
|
|
/**
|
|
* Interprets the `signals` hash and instantiates it
|
|
* @private
|
|
*/
|
|
_loadSignals : function () {
|
|
var signals = this.signals || {};
|
|
sys.each( signals, function ( value, key ) {
|
|
var opts = {};
|
|
if ( !sys.isEmpty( value ) ) {
|
|
if ( sys.isBoolean( value.memorize ) ) {
|
|
opts.memorize = value.memorize;
|
|
}
|
|
if ( sys.isBoolean( value.params ) ) {
|
|
opts.params = value.params;
|
|
}
|
|
if ( !sys.isEmpty( value.context ) ) {
|
|
opts.context = value.context;
|
|
}
|
|
}
|
|
this._addSignal( key, opts );
|
|
} );
|
|
},
|
|
/**
|
|
* Creates a single signal
|
|
* @param {string} name The name of the signal
|
|
* @param {module:mixins/signalable~SignalOptions} options The options the signal expects
|
|
* @private
|
|
*/
|
|
_addSignal : function ( name, options ) {
|
|
if ( sys.isEmpty( this[name] ) ) {
|
|
this[name] = new Signal( this, name, options );
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Add a signal to an object. If any members of the hash already exist in `this.signals`, they will be overwritten.
|
|
* @param {module:mixins/signalable.SignalOptions} signals
|
|
* @private
|
|
*/
|
|
_addSignals : function ( signals ) {
|
|
signals = signals || {};
|
|
if ( this.options ) {signals = sys.extend( {}, sys.result( this, 'signals' ), signals );}
|
|
this.signals = signals;
|
|
},
|
|
/**
|
|
* Clean up
|
|
* @private
|
|
*/
|
|
destroy : function () {
|
|
sys.each( sys.keys( this ), function ( key ) {
|
|
if ( this[key] instanceof Signal || this[key] instanceof signals.Signal ) {
|
|
this[key].close();
|
|
}
|
|
}, this );
|
|
}
|
|
} );
|
|
|
|
module.exports = Signalable;
|
|
alable.Signal = Signal;
|
|
Signalable.mixin = Base.mixin;
|
|
|
|
/**
|
|
* When true, the class will load the `signals` hash and create the signal definitions during construction
|
|
* @memberOf mixins/signalable#
|
|
* @name autoLoadSignals
|
|
* @type boolean
|
|
*/
|
|
|
|
|
|
/**
|
|
* A hash of signals to create automatically. Each definition consists of a name for the signal as the key
|
|
* and then a hash of options (nullable) for each signal
|
|
* @type {hash|function():hash}
|
|
* @memberOf mixins/signalable#
|
|
* @name signals
|
|
* @type module:mixins/signalable.SignalOptions
|
|
*/
|
|
</pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div class="clearfix"></div>
|
|
<footer>
|
|
|
|
|
|
<span class="copyright">
|
|
DocStrap Copyright © 2012-2013 The contributors to the JSDoc3 and DocStrap projects.
|
|
</span>
|
|
<br />
|
|
|
|
<span class="jsdoc-message">
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha5</a>
|
|
on Mon Jul 7th 2014 using the <a
|
|
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
|
|
</span>
|
|
</footer>
|
|
</div>
|
|
|
|
|
|
<br clear="both">
|
|
</div>
|
|
|
|
</div>
|
|
<!--<script src="scripts/sunlight.js"></script>-->
|
|
<script src="scripts/docstrap.lib.js"></script>
|
|
<script src="scripts/bootstrap-dropdown.js"></script>
|
|
<script src="scripts/toc.js"></script>
|
|
|
|
<script>
|
|
$( function () {
|
|
$( "[id*='$']" ).each( function () {
|
|
var $this = $( this );
|
|
|
|
$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
|
|
} );
|
|
|
|
$( "#toc" ).toc( {
|
|
anchorName : function ( i, heading, prefix ) {
|
|
return $( heading ).attr( "id" ) || ( prefix + i );
|
|
},
|
|
selectors : "h1,h2,h3,h4",
|
|
showAndHide : false,
|
|
scrollTo : "100px"
|
|
} );
|
|
|
|
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
|
|
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
|
|
$( '.dropdown-toggle' ).dropdown();
|
|
// $( ".tutorial-section pre, .readme-section pre" ).addClass( "sunlight-highlight-javascript" ).addClass( "linenums" );
|
|
|
|
$( ".tutorial-section pre, .readme-section pre" ).each( function () {
|
|
var $this = $( this );
|
|
|
|
var example = $this.find( "code" );
|
|
exampleText = example.html();
|
|
var lang = /{@lang (.*?)}/.exec( exampleText );
|
|
if ( lang && lang[1] ) {
|
|
exampleText = exampleText.replace( lang[0], "" );
|
|
example.html( exampleText );
|
|
lang = lang[1];
|
|
} else {
|
|
lang = "javascript";
|
|
}
|
|
|
|
if ( lang ) {
|
|
|
|
$this
|
|
.addClass( "sunlight-highlight-" + lang )
|
|
.addClass( "linenums" )
|
|
.html( example.html() );
|
|
|
|
}
|
|
} );
|
|
|
|
Sunlight.highlightAll( {
|
|
lineNumbers : true,
|
|
showMenu : true,
|
|
enableDoclinks : true
|
|
} );
|
|
} );
|
|
</script>
|
|
|
|
|
|
|
|
<!--Navigation and Symbol Display-->
|
|
|
|
|
|
|
|
<!--Google Analytics-->
|
|
|
|
|
|
</body>
|
|
</html>
|