Class: StaticApi

StaticApi

new StaticApi()

The restmod type API, every generated restmod model type exposes this API.

Properties:
Name Type Description
$type object

Reference to the type itself, for compatibility with the ScopeApi

About object creation

Direct construction of object instances using new is not recommended. A collection of static methods are available to generate new instances of a model, for more information read the ModelCollection documentation.

Source:
  • module/factory.js, line 61

Extends

Members

decode

The model decoding strategy

This method is called to populate a record from raw data (unppacked)

Source:
  • module/factory.js, line 299

decodeName

The model name decoding strategy

This method is called on every raw record data property to rename it, by default is not defined.

Override this method to change the property renaming strategy.

Source:
  • module/factory.js, line 322

encode

The model encoding strategy

This method is called to extract raw data from a record to be sent to server (before packing)

Source:
  • module/factory.js, line 308

encodeName

The model name encoding strategy

This method is called when encoding a record to rename the record properties into the raw data properties, by default is not defined.

Override this method to change the property renaming strategy

Source:
  • module/factory.js, line 337

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...)

Inherited From:
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

Inherited From:
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();
Inherited From:
Source:
  • module/api/common-api.js, line 261
Returns:

$q promise

Type
promise

$build(_init) → {RecordApi}

Builds a new instance of this model, does not assign a pk to the created object.

ATTENTION: item will not show in collection until $save is called. To reveal item before than call $reveal.

Parameters:
Name Type Description
_init object

Initial values

Inherited From:
Source:
  • module/api/scope-api.js, line 53
Returns:

single record

Type
RecordApi

$buildRaw(_raw) → {RecordApi}

Builds a new instance of this model using undecoded data.

ATTENTION: does not automatically reveal item in collection, chain a call to $reveal to do so.

Parameters:
Name Type Description
_raw object

Undecoded data

Inherited From:
Source:
  • module/api/scope-api.js, line 67
Returns:

single record

Type
RecordApi

$cancel() → {CommonApi}

Cancels all pending actions registered with $action.

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

self

Type
CommonApi

$collection(_params, _scope) → {CollectionApi}

Builds a new collection bound to this scope.

If scope is another collection then it will inherit its parameters

Collections are bound to an api resource.

Parameters:
Name Type Description
_params object

Additional query string parameters

_scope object

Scope override (optional)

Inherited From:
Source:
  • module/api/scope-api.js, line 111
Returns:

Model Collection

Type
CollectionApi

$create(_attr) → {RecordApi}

Builds and saves a new instance of this model

Parameters:
Name Type Description
_attr object

Data to be saved

Inherited From:
Source:
  • module/api/scope-api.js, line 94
Returns:

single record

Type
RecordApi

$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.

Inherited From:
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

Inherited From:
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');
        });
      });
    });
  }
})
Inherited From:
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

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

self

Type
CommonApi

$find(_pk, _params) → {RecordApi}

Attempts to resolve a resource using provided private key.

Parameters:
Name Type Description
_pk mixed

Private key

_params object

Additional query parameters

Inherited From:
Source:
  • module/api/scope-api.js, line 82
Returns:

single record

Type
RecordApi

$hasPendingActions() → {Boolean}

Returns true if object has queued actions

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

Object request pending status.

Type
Boolean

$new(_pk, _scope) → {RecordApi}

Builds a new instance of this model, bound to this instance scope, sets its primary key.

Parameters:
Name Type Description
_pk mixed

object private key

_scope object

scope override (optional)

Inherited From:
Source:
  • module/api/scope-api.js, line 39
Returns:

New model instance

Type
RecordApi

$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

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

self

Type
CommonApi

$search(_params) → {CollectionApi}

Generates a new collection bound to this context and url and calls $fetch on it.

Parameters:
Name Type Description
_params object

Collection parameters

Inherited From:
Source:
  • module/api/scope-api.js, line 123
Returns:

record collection

Type
CollectionApi

$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)

Inherited From:
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

Inherited From:
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)

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

The resource url.

Type
string

$urlFor(_resource) → {string|null}

provides urls for scope's resources.

Parameters:
Name Type Description
_resource mixed

The target resource.

Inherited From:
Source:
  • module/api/scope-api.js, line 24
Returns:

The url or nill if resource does not meet the url requirements.

Type
string | null

encodeUrlName() → {string}

The model name to url encoding strategy

This method is called when translating a name into an url fragment (mainly by relations).

By default it uses the inflector.parameterize method, in 1.2 this will change and the default behaviour will be to do nothing.

Source:
  • module/factory.js, line 352
Returns:

url fragment

Type
string

getProperty(_key, _default) → {mixed}

Gets a model's internal property value.

Some builtin properties: url urlPrefix * primaryKey

Parameters:
Name Type Description
_key string

Property name

_default mixed

Value to return if property is not defined

Source:
  • module/factory.js, line 153
Returns:

value

Type
mixed

identity() → {boolean}

Returns the model API name.

This name should match the one used throughout the API. It's only used by some extended functionality, like the default packer.

By default model name is infered from the url, but for nested models and special cases it should be manually set by writing the name and plural properties:

restmod.model(null, {
  __name__: 'resource',
  __plural__: 'resourciness' // set only if inflector cant properly gess the name.
});
Source:
  • module/factory.js, line 241
Returns:
  • If true, return plural name

    Type
    boolean
  • The base url.

    Type
    string

inferKey(_rawData) → {mixed}

Extracts the primary key from raw record data.

Uses the key configured in the PRIMARY_KEY variable or 'id' by default.

Some considerations: This method can be overriden to handle other scenarios. This method should not change the raw data passed to it. * The primary key value extracted by this method should be comparable using the == operator.

Parameters:
Name Type Description
_rawData string

Raw object data (before it goes into decode)

Source:
  • module/factory.js, line 132
Returns:

The primary key value.

Type
mixed

isNested() → {boolean}

Returns true if model is nested.

An nested model can only be used as a nested resource (using hasMany or hasOne relations)

Source:
  • module/factory.js, line 167
Returns:

true if model is nested.

Type
boolean

mix() → {Model}

Modifies model behavior.

Source:
  • module/factory.js, line 253
Returns:

The model

Type
Model

pack() → {mixed}

The model packing strategy

This method is called to wrap raw record data to be sent in a request.

Override this method to change the request packing strategy, by default its a noop

Source:
  • module/factory.js, line 290
Returns:

Wrapped data

Type
mixed

single(_url) → {Model}

Returns a resource bound to a given url, with no parent scope.

This can be used to create singleton resources:

module('BikeShop', []).factory('Status', function(restmod) {
  return restmod.model(null).$single('/api/status');
};)
Parameters:
Name Type Description
_url string

Url to bound resource to.

Source:
  • module/factory.js, line 187
Returns:

new resource instance.

Type
Model

unpack() → {mixed}

The model unpacking strategy

This method is called to extract record data from a request response, its also responsible of handling the response metadata.

Override this method to change the metadata processing strategy, by default its a noop

Source:
  • module/factory.js, line 275
Returns:

Resource raw data

Type
mixed