bedrock-angular-model
Bedrock AngularJS Model Utilities
An AngularJS module that provides a service with utility functions for manipulating models. It is primarily used to update objects and objects in arrays in-place, to help simplify watch code, etc.
Quick Examples
angular; /* @ngInject */ { return restrict: 'E' scope: {} { var model = oldItem: {} anArray: ; scope { // replace `oldItem` keys with key-value pairs from newItem brModelService; }; scope { // replace existing item with `newItem` when `id` property matches // otherwise push `newItem` onto the array brModelService; }; scope { // overwrite matching items in an array if they exist, otherwise // push them onto the array brModelService; }; scope { // remove an item from an array when its `id` matches brModelService; }; scope { // remove all items that match `key` from an array brModelService; }; } ;}
Setup
npm install bedrock-angular-model
If you're using bedrock-angular, installation of the module followed by a restart of your bedrock server is sufficient to make the directive available to your application.
To manually add bedrock-angular-model as a dependency:
angular;
API
brModelService.replace(dst, src, [fn])
Updates an existing object in-place. For every key in src
, the value in dst
associated with the same key is updated in-place (recursively) with the value
from src
. If fn
is given, it will be used as a comparison function for
replacing objects in arrays. Any keys that aren't in src
are deleted from
dst
.
brModelService.replaceInArray(dst, src, fn)
Updates an object in-place in an array. fn
will be called for each element in
dst
, until it returns true
. If it returns true
, then the related element
from dst
will be updated in-place to match src
. If fn
never returns
true
for any element of dst
, then src
is pushed onto dst
.
brModelService.replaceArray(dst, src, fn)
Updates an array in-place using the elements from another array. For each
element in src
, a matching element in dst
will be sought out using fn
.
If fn
returns true
when comparing an element from src
and an element
from dst
, then the element in dst
will be updated in-place with the element
from src
. If there is no matching element in dst
, the element from src
will be pushed onto dst
. If there are any elements in dst
that have no
match in src
, they will be removed.
brModelService.removeFromArray(array, fn)
Updates an array in-place by removing a specific element. fn
will be called
for each element in array
, until it returns true
. If fn
returns true
,
then the element will be removed from array
.
brModelService.removeAllFromArray(array, fn)
Updates an array in-place by removing all elements that match a particular
criteria. fn
will be called for each element in array
. If fn
returns
true
, the element will be removed from array
.