|
2 | 2 | <html lang="en">
|
3 | 3 | <head>
|
4 | 4 | <meta charset="utf-8">
|
5 |
| - <meta name="description" content="[todo] Document a mix-in object."><title>Use JSDoc: @mixin</title> |
| 5 | + <meta name="description" content="Document a mix-in object."><title>Use JSDoc: @mixin</title> |
6 | 6 |
|
7 | 7 | <link rel="stylesheet" href="lib/prettify.css" />
|
8 | 8 | <script src="lib/prettify.js"></script>
|
|
176 | 176 | <article>
|
177 | 177 | <h1>@mixin</h1>
|
178 | 178 |
|
| 179 | + |
| 180 | +<h3>Syntax</h3> |
| 181 | +<code>@mixin [<MixinName>]</code> |
| 182 | + |
179 | 183 | <h3>Overview</h3>
|
180 | 184 |
|
181 | 185 | <p>
|
| 186 | +A mixin is a class providing functionality that is intended to be added in to other classes desiring that functionality. |
| 187 | +The @mixin tag allows the user to mark an object as a mixin. |
| 188 | +</p> |
| 189 | + |
| 190 | +<p> |
| 191 | +For example, you might have an "Eventful" mixin which provides event handling methods to other classes, but isn't intended to be used itself. |
182 | 192 | </p>
|
183 | 193 |
|
184 | 194 | <h3>Examples</h3>
|
185 | 195 |
|
| 196 | +<dl class="example"> |
| 197 | +<dt>Example of a @mixin</dt> |
| 198 | +<dd> |
| 199 | +<pre class="prettyprint lang-js"> |
| 200 | +/** |
| 201 | + * This provides methods used for event handling. It's not meant to |
| 202 | + * be used directly, except as a provider of related methods. |
| 203 | + * |
| 204 | + * @mixin |
| 205 | + */ |
| 206 | +var Eventful = { |
| 207 | + /** Register a handler function to be called whenever this event is fired. |
| 208 | + * @param {string} eventName - name of the event |
| 209 | + * @param {Eventful~eventHandler} handler - the handler to be called. |
| 210 | + */ |
| 211 | + on: function(eventName, handler) { |
| 212 | + // code... |
| 213 | + }, |
| 214 | + |
| 215 | + /** Fires an event, causing all handlers for that event name to run. |
| 216 | + * @param {string} eventName - name of the event |
| 217 | + * @param {object} eventData - passed into each handler. |
| 218 | + */ |
| 219 | + fire: function(eventName, eventData) { |
| 220 | + // code... |
| 221 | + } |
| 222 | +}; |
| 223 | +/** Used as an event callback {@link Eventful.on}. |
| 224 | + * @callback Eventful~eventHandler |
| 225 | + * @param {object} eventData - the event data. |
| 226 | + */ |
| 227 | + |
| 228 | +</pre> |
| 229 | +</dd> |
| 230 | +</dl><p> |
| 231 | +In the above we document a set of methods, Eventful, that can be mixed in to other classes to give them the ability to fire and listen to events. |
| 232 | +(Note also the use of the <a href="tags-callback.html">@callback</a> tag to document the event handler parameter in the "on" function - we could have just written "@param {function(object)} handler" instead if we didn't want to use that). |
| 233 | +</p> |
| 234 | + |
186 | 235 | <p>
|
| 236 | +Use <a href="tags-mixes.html">@mixes</a> to indicate in the documentation that another class mixes in Eventful (you are still responsible for doing the actual mixing in the code, however). Note, this doesn't support references like "{@link FormButton#on}" (yet). |
187 | 237 | </p>
|
188 | 238 |
|
189 | 239 | <dl class="example">
|
190 |
| -<dt>Example goes here</dt> |
| 240 | +<dt>Having another class @mixes Eventful</dt> |
191 | 241 | <dd>
|
192 | 242 | <pre class="prettyprint lang-js">
|
193 |
| -// todo |
| 243 | +/** |
| 244 | + * @constructor FormButton |
| 245 | + * @mixes Eventful |
| 246 | + */ |
| 247 | +var FormButton = function() { |
| 248 | + // code... |
| 249 | +}; |
| 250 | +FormButton.prototype.press = function() { |
| 251 | + this.fire('press', {}); |
| 252 | +} |
| 253 | +mix(Eventful).into(FormButton.prototype); |
| 254 | + |
| 255 | +function mix(source) { |
| 256 | + return {into: function(target) { |
| 257 | + for (var property in source) { |
| 258 | + if( !(property in {}) ) { |
| 259 | + target[property] = source[property] |
| 260 | + } |
| 261 | + } |
| 262 | + return this; |
| 263 | + }}; |
| 264 | +} |
194 | 265 |
|
195 | 266 | </pre>
|
196 | 267 | </dd>
|
197 | 268 | </dl><h3>See Also</h3>
|
198 | 269 |
|
199 | 270 | <ul>
|
200 |
| - <li><a href="#">...</a></li> |
201 |
| -</ul> |
| 271 | + <li><a href="tags-mixes.html">@mixes</a></li> |
| 272 | + <li><a href="tags-borrows.html">@borrows</a></li> |
| 273 | + <li><a href="tags-constructor.html">@class</a></li> |
| 274 | +</ul> |
| 275 | + |
202 | 276 | </article>
|
203 | 277 |
|
204 | 278 | <footer>
|
|
0 commit comments