Class: CommonApi

CommonApi

new CommonApi()

Provides a common framework for restmod resources.

This API is included in RecordApi and CollectionApi. making its methods available in every structure generated by restmod.

TODO: Describe hook mechanism, promise mechanism and send lifecycle.

Properties:
Name Type Description
$promise promise

The last operation promise (undefined if no promise has been created yet)

$pending array

Pending requests associated to this resource (undefined if no request has been initiated)

$$cb object

Scope call backs (undefined if no callbacks have been defined, private api)

$$dsp function

The current event dispatcher (private api)

Source:
  • module/api/common-api.js, line 23

Methods

$action() → {CommonApi}

Registers a new action to be executed in the promise queue.

Registered pending actions can be canceled using $cancel

$cancel will also cancel any ongoing call to $send (will not abort it yet though...)

Source:
  • module/api/common-api.js, line 413
Returns:

self

Type
CommonApi

$always(_fun) → {CommonApi}

Promise chaining method, similar to then but executes same callback in success or error.

Usage:

col.$fetch().$always(function() { });
Parameters:
Name Type Description
_fun function

success/error callback

Source:
  • module/api/common-api.js, line 314
Returns:

self

Type
CommonApi

$asPromise() → {promise}

Returns this object last promise.

If promise does not exist, then a new one is generated that resolves to the object itsef. The new promise is not set as the current object promise, for that use $then.

Usage:

col.$fetch().$asPromise();
Source:
  • module/api/common-api.js, line 261
Returns:

$q promise

Type
promise

$cancel() → {CommonApi}

Cancels all pending actions registered with $action.

Source:
  • module/api/common-api.js, line 446
Returns:

self

Type
CommonApi

$decorate(_hooks, _fun) → {CommonApi}

Registers hooks to be used only inside the given function (decorated context).

// special fetch method that sends a special token header.
restmod.mixin({
  $fetchWithToken: function(_token) {
    return this.$decorate({
      'before-fetch': function(_req) {
        _req.headers = _req.headers || {};
        _req.headers['Token'] = _token;
      }
    ), function() {
      return this.$fetch();
    })
  }
});
Parameters:
Name Type Description
_hooks object | function

Hook mapping object or hook execution method.

_fun function

Function to be executed in with decorated context, this function is executed in the callee object context.

Source:
  • module/api/common-api.js, line 188
Returns:

self

Type
CommonApi

$dispatch(_hook, _args, _ctx) → {CommonApi}

Executes a given hook callbacks using the current dispatcher context.

This method can be used to provide custom object lifecycle hooks.

Usage:

var mixin = restmod.mixin({
  triggerDummy: function(_param) {
    this.$dispatch('dummy-hook', _param);
  }
});

// Then hook can be used at model definition to provide type-level customization:
var Bike $resmod.model('/api/bikes', mixin, {
  '~dummy-hook': function() {
    alert('This is called for every bike');
  }
};

// or at instance level:
var myBike = Bike.$build();
myBike.$on('dummy-hook', function() {
  alert('This is called for myBike only');
});

// or event at decorated context level
myBike.$decorate({
  'dummy-hook': function() {
    alert('This is called for myBike only inside the decorated context');
  }
}, fuction() {
 // decorated context
});
Parameters:
Name Type Description
_hook string

Hook name

_args array

Hook arguments

_ctx object

Hook execution context override

Source:
  • module/api/common-api.js, line 108
Returns:

self

Type
CommonApi

$dispatcher() → {function}

Retrieves the current object's event dispatcher function.

This method can be used in conjuction with $decorate to provide a consistent hook context during async operations. This is important when building extensions that want to support the contextual hook system in asynchronic operations.

For more information aboout contextual hooks, see the CommonApi#decorate documentation.

Usage:

restmod.mixin({
  $saveAndTrack: function() {
    var dsp = this.$dispatcher(), // capture the current dispatcher function.
        self = this;
    this.$save().$then(function() {
      this.$send({ path: '/traces', data: 'ble' }, function() {
        this.$decorate(dsp, function() {
          // the event is dispatched using the dispatcher function available when $saveAndTrack was called.
          this.$dispatch('trace-stored');
        });
      });
    });
  }
})
Source:
  • module/api/common-api.js, line 239
Returns:

Dispatcher evaluator

Type
function

$finally(_cb) → {CommonApi}

Promise chaining, keeps the model instance as the chain context.

Calls ´$q.finally´ on the collection's last promise, updates last promise with finally result.

Usage:

col.$fetch().$finally(function() { });
Parameters:
Name Type Description
_cb function

callback

Source:
  • module/api/common-api.js, line 334
Returns:

self

Type
CommonApi

$hasPendingActions() → {Boolean}

Returns true if object has queued actions

Source:
  • module/api/common-api.js, line 464
Returns:

Object request pending status.

Type
Boolean

$on(_hook, _fun) → {CommonApi}

Registers an instance hook.

An instance hook is called only for events generated by the calling object.

var bike = Model.$build(), bike2 = Model.$build();
bike.$on('before-save', function() { alert('saved!'); });

bike.$save(); // 'saved!' alert is shown after bike is saved
bike2.$save(); // no alert is shown after bike2 is saved
Parameters:
Name Type Description
_hook string

Hook name

_fun function

Callback

Source:
  • module/api/common-api.js, line 157
Returns:

self

Type
CommonApi

$send(_options, _success, _error) → {CommonApi}

Low level communication method, wraps the $http api.

  • You can access last request promise using the $asPromise method.
  • Pending requests will be available at the $pending property (array).
  • Current request execution status can be queried using the $status property (current request, not last).
  • The $status property refers to the current request inside $send _success and _error callbacks.
Parameters:
Name Type Description
_options object

$http options

_success function

sucess callback (sync)

_error function

error callback (sync)

Source:
  • module/api/common-api.js, line 356
Returns:

self

Type
CommonApi

$then(_success, _error) → {CommonApi}

Promise chaining method, keeps the model instance as the chain context.

Calls $q.then on the model's last promise.

Usage:

col.$fetch().$then(function() { });
Parameters:
Name Type Description
_success function

success callback

_error function

error callback

Source:
  • module/api/common-api.js, line 286
Returns:

self

Type
CommonApi

$url(_for) → {string}

Gets this resource url.

Parameters:
Name Type Description
_for string

Intended usage for the url (optional)

Source:
  • module/api/common-api.js, line 50
Returns:

The resource url.

Type
string