new CollectionApi()
A restmod collection is an extended array type bound REST resource route.
Every time a new restmod model is created, an associated collection type is created too.
TODO: talk about fetch/refresh behaviour, lifecycles, collection scopes, adding/removing
For $fetch on a collection:
- before-fetch-many
- before-request
- after-request[-error]
- after-feed (called for every record if no errors)
- after-feed-many (only called if no errors)
- after-fetch-many[-error]
- Source:
- module/api/collection-api.js, line 7
Properties:
| Name | Type | Description |
|---|---|---|
$isCollection |
boolean | Helper flag to separate collections from the main type |
$scope |
object | The collection scope (hierarchical scope, not angular scope) |
$params |
object | The collection query parameters |
Extends
Methods
-
$action() → {CommonApi}
-
Registers a new action to be executed in the promise queue.
Registered pending actions can be canceled using
$cancel$cancelwill 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
-
$add(_obj) → {CollectionApi}
-
Adds an item to the back of the collection. This method does not attempt to send changes to the server. To create a new item and add it use $create or $build.
Triggers after-add callbacks.
Parameters:
Name Type Description _objRecordApi Item to be added
- Source:
- module/api/collection-api.js, line 150
Returns:
self
- Type
- CollectionApi
-
$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 _funfunction 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
$saveis called. To reveal item before than call$reveal.Parameters:
Name Type Description _initobject 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 _rawobject 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
-
$clear() → {CollectionApi}
-
Resets the collection's contents
- Source:
- module/api/collection-api.js, line 99
Returns:
self
- Type
- CollectionApi
-
$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 _paramsobject Additional query string parameters
_scopeobject 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 _attrobject Data to be saved
- Inherited From:
- Source:
- module/api/scope-api.js, line 94
Returns:
single record
- Type
- RecordApi
-
$decode(_raw, _mask) → {CollectionApi}
-
Feeds raw collection data into the collection.
This method is for use in collections only.
Parameters:
Name Type Description _rawarray Data to add
_maskstring 'CRU' mask
- Source:
- module/api/collection-api.js, line 62
Returns:
self
- Type
- CollectionApi
-
$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 _hooksobject | function Hook mapping object or hook execution method.
_funfunction 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 _hookstring Hook name
_argsarray Hook arguments
_ctxobject 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
$decorateto 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
-
$encode(_mask) → {CollectionApi}
-
Encodes array data into a its serialized version.
Parameters:
Name Type Description _maskstring 'CRU' mask
- Source:
- module/api/collection-api.js, line 82
Returns:
self
- Type
- CollectionApi
-
$fetch(_params) → {CollectionApi}
-
Begin a server request to populate collection. This method does not clear the collection contents by default, use
$refreshto reset and fetch.This method is for use in collections only.
Parameters:
Name Type Description _paramsobject | function Additional request parameters, not stored in collection, if a function is given, then it will be called with the request object to allow requet customization.
- Source:
- module/api/collection-api.js, line 117
Returns:
self
- Type
- CollectionApi
-
$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 _cbfunction 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 _pkmixed Private key
_paramsobject 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
-
$indexOf(_obj) → {number}
-
Finds the location of an object in the array.
If a function is provided then the index of the first item for which the function returns true is returned.
Parameters:
Name Type Description _objRecordApi | function Object to find
- Source:
- module/api/collection-api.js, line 200
Returns:
Object index or -1 if not found
- Type
- number
-
$initialize()
-
Called by collection constructor on initialization.
Note: Is better to add a hook on after-init than overriding this method.
- Source:
- module/api/collection-api.js, line 46
-
$new(_pk, _scope) → {RecordApi}
-
Builds a new instance of this model, bound to this instance scope, sets its primary key.
Parameters:
Name Type Description _pkmixed object private key
_scopeobject 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 savedParameters:
Name Type Description _hookstring Hook name
_funfunction Callback
- Inherited From:
- Source:
- module/api/common-api.js, line 157
Returns:
self
- Type
- CommonApi
-
$remove(_obj) → {CollectionApi}
-
Removes an item from the collection.
This method does not send a DELETE request to the server, it just removes the item locally. To remove an item AND send a DELETE use the item's $destroy method.
Triggers after-remove callbacks.
Parameters:
Name Type Description _objRecordApi Item to be removed
- Source:
- module/api/collection-api.js, line 179
Returns:
self
- Type
- CollectionApi
-
$search(_params) → {CollectionApi}
-
Generates a new collection bound to this context and url and calls $fetch on it.
Parameters:
Name Type Description _paramsobject 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
$asPromisemethod. - 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
_successand_errorcallbacks.
Parameters:
Name Type Description _optionsobject $http options
_successfunction sucess callback (sync)
_errorfunction error callback (sync)
- Inherited From:
- Source:
- module/api/common-api.js, line 356
Returns:
self
- Type
- CommonApi
- You can access last request promise using the
-
$then(_success, _error) → {CommonApi}
-
Promise chaining method, keeps the model instance as the chain context.
Calls
$q.thenon the model's last promise.Usage:
col.$fetch().$then(function() { });Parameters:
Name Type Description _successfunction success callback
_errorfunction 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 _forstring 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 _resourcemixed 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