API Docs for: undefined
Show:

File: ../gallery-async-command/js/async-command.js

  1. /**
  2.  * @module gallery-async-command
  3.  */
  4. (function (Y, moduleName) {
  5.     'use strict';
  6.    
  7.     var _string_args = 'args',
  8.         _string_complete = 'complete',
  9.         _string_failure = 'failure',
  10.         _string_initOnly = 'initOnly',
  11.         _string_start = 'start',
  12.         _string_success = 'success',
  13.        
  14.         _Base = Y.Base,
  15.        
  16.         _createCompleteFunction,
  17.         _false = false,
  18.         _isArray = Y.Lang.isArray,
  19.         _true = true;
  20.    
  21.     /**
  22.     * Asynchronous command class.
  23.     * @class AsyncCommand
  24.     * @extends Base
  25.     * @param {Object} config Configuration Object.
  26.     */
  27.     Y.AsyncCommand = _Base.create(moduleName, _Base, [], {
  28.         initializer: function () {
  29.             var me = this;

  30.             /**
  31.             * Fired when the command function completes.
  32.             * @event complete
  33.             * @fireonce
  34.             * @param error Optional error value.
  35.             * @param {Boolean} failed Indicates the failed status of the
  36.             * command.
  37.             * @param value Optional return value from the command function.
  38.             */
  39.             me.publish(_string_complete, {
  40.                 defaultFn: function () {
  41.                     me._set('completed', _true);
  42.                 },
  43.                 fireOnce: _true
  44.             });

  45.             /**
  46.             * Fired when the command function fails.
  47.             * @event failure
  48.             * @fireonce
  49.             * @param error Optional error value.
  50.             * @protected
  51.             */
  52.             me.publish(_string_failure, {
  53.                 defaultFn: function (eventFacade) {
  54.                     var error = eventFacade.error;

  55.                     me._set('error', error);
  56.                     me._set('failed', _true);

  57.                     me.fire(_string_complete, {
  58.                         error: error,
  59.                         failed: _true
  60.                     });
  61.                 },
  62.                 fireOnce: _true
  63.             });

  64.             /**
  65.             * Fired when the command function starts.
  66.             * @event start
  67.             * @fireonce
  68.             * @protected
  69.             */
  70.             me.publish(_string_start, {
  71.                 defaultFn: function () {
  72.                     me._set('started', _true);
  73.                     me.get('fn').apply(me.get('ctx'), me.get(_string_args));
  74.                 },
  75.                 fireOnce: _true
  76.             });

  77.             /**
  78.             * Fired when the command function succeeds.
  79.             * @event success
  80.             * @fireonce
  81.             * @param value Optional return value from the command function.
  82.             * @protected
  83.             */
  84.             me.publish(_string_success, {
  85.                 defaultFn: function (eventFacade) {
  86.                     var value = eventFacade.value;

  87.                     me._set('value', value);

  88.                     me.fire(_string_complete, {
  89.                         failed: _false,
  90.                         value: value
  91.                     });
  92.                 },
  93.                 fireOnce: _true
  94.             });

  95.             me.get(_string_args).unshift(_createCompleteFunction(me));
  96.         },
  97.         /**
  98.         * Execute the command function.
  99.         * @method run
  100.         * @chainable
  101.         */
  102.         run: function () {
  103.             this.fire(_string_start);
  104.             return this;
  105.         }
  106.     }, {
  107.         ATTRS: {
  108.             /**
  109.             * Array of arguments to be passed to the command function.
  110.             * A special callback function is automatically added as the first
  111.             * argument.
  112.             * @attribute args
  113.             * @default []
  114.             * @initonly
  115.             * @type Array
  116.             */
  117.             args: {
  118.                 setter: function (args) {
  119.                     if (!_isArray(args)) {
  120.                         args = [
  121.                             args
  122.                         ];
  123.                     }

  124.                     return args;
  125.                 },
  126.                 value: [],
  127.                 writeOnce: _string_initOnly
  128.             },
  129.             /**
  130.             * Boolean value indicating the completed status of the command.
  131.             * @attribute completed
  132.             * @default false
  133.             * @readonly
  134.             * @type Boolean
  135.             */
  136.             completed: {
  137.                 readOnly: _true,
  138.                 value: _false
  139.             },
  140.             /**
  141.             * Execution context for the command function.
  142.             * @attribute ctx
  143.             * @initonly
  144.             */
  145.             ctx: {
  146.                 value: null,
  147.                 writeOnce: _string_initOnly
  148.             },
  149.             /**
  150.             * Error value passed to the failure event.
  151.             * @attribute error
  152.             * @readonly
  153.             */
  154.             error: {
  155.                 readOnly: _true,
  156.                 value: null
  157.             },
  158.             /**
  159.             * Boolean value indicating the failed status of the command.
  160.             * @attribute failed
  161.             * @default false
  162.             * @readonly
  163.             * @type Boolean
  164.             */
  165.             failed: {
  166.                 readOnly: _true,
  167.                 value: _false
  168.             },
  169.             /**
  170.             * The command function to execute.  This function receives a special
  171.             * success callback function as the first parameter.  The success
  172.             * callback function has a method parameter called fail.  One of
  173.             * these callback functions must be called in order to complete the
  174.             * command.
  175.             * @attribute fn
  176.             * @initonly
  177.             * @type Function
  178.             */
  179.             fn: {
  180.                 value: function (success) {
  181.                     success();
  182.                 },
  183.                 writeOnce: _string_initOnly
  184.             },
  185.             /**
  186.             * Boolean value indicating the started status of the command.
  187.             * @attribute started
  188.             * @default false
  189.             * @readonly
  190.             * @type Boolean
  191.             */
  192.             started: {
  193.                 readOnly: _true,
  194.                 value: _false
  195.             },
  196.             /**
  197.             * Value passed to the success event.
  198.             * @attribute value
  199.             * @readonly
  200.             */
  201.             value: {
  202.                 readOnly: _true,
  203.                 value: null
  204.             }
  205.         }
  206.     });
  207.    
  208.     _createCompleteFunction = function (asyncCommand) {
  209.         var successFunction = function (value) {
  210.             asyncCommand.fire(_string_success, {
  211.                 value: value
  212.             });
  213.         };
  214.        
  215.         successFunction.fail = function (error) {
  216.             asyncCommand.fire(_string_failure, {
  217.                 error: error
  218.             });
  219.         };
  220.        
  221.         return successFunction;
  222.     };
  223. }(Y, arguments[1]));
  224.