perform.js

0.1.0 • Public • Published

perform.js Build Status

Global utility for establishing, managing, and queuing actions with dependencies.

API

perform(name)

Wrapper function to return an action instance.

PARAMETERS:
  • *name: The name of the action you would like to perform.
RETURNS:

An instance of action, set with the provided name. The action instance is meant to be used with the ifSatisfied or whenSatisfied methods.

EXAMPLE USAGE:
perform('view:model:update').ifSatisfied(function (done) {
  // Do something
  done();
})

perform.addDependencies(map)

Add dependencies to actions.

PARAMETERS:
  • *map: A key value pairs where the key represents the action name and the value is an array holding the specified action dependencies.
EXAMPLE USAGE:
perform.addDependencies({
  'view:model:save': ['view:model:create']
});

perform.restart(name)

Reset the state for the specified name. The passed name is namepsace aware. Passing in view:model would reset all child actions (view:model:save, view:model:create, etc..)

PARAMETERS:
  • *name: The name of the action you would like to perform.
EXAMPLE USAGE:
perform.restart('view');

Action.isSatisfied(fn, context)

Perform the specified action only if all dependencies are satisfied.

PARAMETERS:
  • *fn: The action you would like to execute if dependencies are satisfied.
  • context: The context in which to call the provided fn.
RETURNS:

Instance of action.

EXAMPLE USAGE:
perform('view:model:save').ifSatisfied(function (done) {
  // Do something
  done();
})

Action.whenSatisfied(fn, context)

Perform the specified action when all dependencies are satisfied.

PARAMETERS:
  • *fn: The action you would like to execute when dependencies are satisfied.
  • context: The context in which to call the provided fn.
RETURNS:

Instance of action.

EXAMPLE USAGE:
perform('view:model:update').whenSatisfied(function (done) {
  // Do something
  done();
});

Action.done();

Update the state of the performed action. This is also passed as the first argument into your action fn. This can be useful for sync operations.

EXAMPLE USAGE:
perform('view:model:displayErr').ifSatisfied(function () {
  // Do something
}).done();

Example Usage

perform.addDependencies({
  'view:model:update': ['view:model:get']
});
var view = {
  initialize  onShow: function () {
    perform.restart('view');
  },
  
  onClose: function () {
    perform('view:model:update).whenSatisfied(function (done) {
      var $input = $('.input-selector');
      
      // If model has changed save
    if (model.attributes.body !== $input.val()) {
      model.update({ success: done });
    } else {
      done();
    }
    });
  }
};

TESTS

Install Dependencies

npm install

Run/View

grunt test

License

The MIT License (MIT) Copyright (c) 2014 First Opinion

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i perform.js

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • jaridmargolin