new ResourceStore(resource, managedInstances, parentStore)
Constructor function for a resource store. A resource store manages inserts, updates and
deletes of instances, can create sub-stores that commit changes to the parent store, and
sets up relations between resource types (e.g. to update reference keys).
Parameters:
Name | Type | Description |
---|---|---|
resource |
ResourceFactoryService | Resource service instance |
managedInstances |
Array.<ResourceInstance> | List of resource instances to manage |
parentStore |
ResourceStore | null | The store of which the new store is a sub-store of |
- Source:
Example
// Basic usage of a resource store
inject(function (ResourceFactoryService, $q) {
var
service = ResourceFactoryService('TestResourceService', 'http://test/:pk/'),
instance1 = service.new(),
instance2 = service.new(),
instance3 = service.new(),
store = service.createStore([instance1, instance2, instance3]);
$httpBackend.expect('DELETE', 'http://test/2/').respond(204, '');
$httpBackend.expect('PATCH', 'http://test/1/').respond(200, {pk: 1});
$httpBackend.expect('POST', 'http://test/').respond(201, {pk: 2});
instance1.pk = 1;
instance2.pk = 2;
store.remove(instance2);
store.persist(instance1);
store.persist(instance3);
$q.when()
.then(function () {
return store.execute();
})
.then(done);
$httpBackend.flush();
});
Methods
addAfterPersistListener(fn)
Adds a after-persist listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
addAfterRemoveListener(fn)
Adds a after-remove listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
addBeforePersistListener(fn)
Adds a before-persist listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
addBeforeRemoveListener(fn)
Adds a before-remove listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
createChildStore(instances) → {ResourceStore}
Creates a new child store from the current store. This store can make changes
to it's managed instances and it will not affect the current stores
instances until the child store commits.
Parameters:
Name | Type | Description |
---|---|---|
instances |
ResourceInstance | Array.<ResourceInstance> | Instances to manage on the child store (default: all instances on the parent store) |
- Source:
Returns:
New child store
- Type
- ResourceStore
createRelation(config) → {ResourceStoreRelation}
Adds a relation to another store. Relations do automatically update foreign key attributes on
instances on a related store when certain events occur. These events include:
remove
: An instance was removed on the server (e.g. by a DELETE call)dispose
: An instances was removed for forgot on the store (e.g. byremove
orforget
methods on the store)update
: An instance got a new PK from the server after saving
You can either use pre-defined behaviours for these events, or hook in custom functions. The pre-defined
behaviours are:
"forget"
: Forget the referencing instance on the related store"null"
: Set the foreign key attribute on the referencing instance tonull
"update"
: Set the foreign key attribute on the referencing instance to the new PK"ignore"
: Do nothing
Parameters:
Name | Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
config |
Object | Configuration object Properties
|
- Source:
Returns:
New relation
execute(clearAfteropt) → {Promise}
Execute the change queue and clears the change queue if clearAfter is set to true (default).
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
clearAfter |
<optional> |
Clear change queue on the store after executing (default: |
- Source:
Returns:
- Type
- Promise
executeAll(clearAfteropt) → {Promise}
Executes the change queue on this an all related stores and clears the change queue if clearAfter is
set to true.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
clearAfter |
<optional> |
Clear change queue on all stores after executing (default: |
- Source:
Returns:
- Type
- Promise
forget(oldInstances) → {Promise}
Forget (un-manage) given instances. The instances object may be a ng-resource result,
a promise, a list of instances or a single instance.
Parameters:
Name | Type | Description |
---|---|---|
oldInstances |
ResourceInstance | Array.<ResourceInstance> | Instances the store should forget |
- Source:
Returns:
Promise that resolves if all given instances are resolved
- Type
- Promise
getByInstance(instance) → {ResourceInstance|undefined}
Gets the managed instance from the store that matches the given
instance (which might by an copy that is not managed or managed by
another store). The instances are matched by their PK attribute.
Parameters:
Name | Type | Description |
---|---|---|
instance |
ResourceInstance | Instance to search for in the store |
- Source:
Returns:
Resource instance managed by the store matching the given instance that might by not managed by the store
- Type
- ResourceInstance | undefined
getByPk(pkValue) → {ResourceInstance|undefined}
Gets the managed instance from the store that matches the given
PK attribute value.
Parameters:
Name | Type | Description |
---|---|---|
pkValue |
String | int |
- Source:
Returns:
Resource instance managed by the store with the given PK value
- Type
- ResourceInstance | undefined
getManagedInstances() → {Array.<ResourceInstance>}
Gets a list of instances visible for the user.
- Source:
Returns:
Instances managed by the store
- Type
- Array.<ResourceInstance>
getPersistQueue() → {Array.<ResourceInstance>}
Gets a list of instances marked for persist.
- Source:
Returns:
Instances marked for persistence
- Type
- Array.<ResourceInstance>
getRemoveQueue() → {Array.<ResourceInstance>}
Gets a list of instances marked for remove.
- Source:
Returns:
Instances marked for removal
- Type
- Array.<ResourceInstance>
getResourceService() → {ResourceFactoryService}
Gets the managed resource service.
- Source:
Returns:
Resource service instance
getSaveQueue() → {Array.<ResourceInstance>}
Gets a list of instances marked for save (e.g. insert).
- Source:
Returns:
Instances marked for save
- Type
- Array.<ResourceInstance>
getUpdateQueue() → {Array.<ResourceInstance>}
Gets a list of instances marked for update.
- Source:
Returns:
Instances marked for update
- Type
- Array.<ResourceInstance>
getVisibleQueue() → {Array.<ResourceInstance>}
Gets a list of instances visible for the user (e.g. that are not marked for removal).
- Source:
Returns:
Instances visible for the user
- Type
- Array.<ResourceInstance>
manage(newInstances) → {Promise}
Manage given instances. The new instances object may be a ng-resource result,
a promise, a list of instances or a single instance.
Parameters:
Name | Type | Description |
---|---|---|
newInstances |
ResourceInstance | Array.<ResourceInstance> | List of instances to manage |
- Source:
Returns:
Promise that resolves if all given instances are resolved
- Type
- Promise
new(paramsopt) → {ResourceInstance}
Returns a new instance managed by the store.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
params |
Object |
<optional> |
Object holding attributes to set on the resource instance |
- Source:
Returns:
New resource instance that is managed by the store
- Type
- ResourceInstance
persist(instances)
Queues given instance for persistence.
Parameters:
Name | Type | Description |
---|---|---|
instances |
ResourceInstance | Array.<ResourceInstance> | Instances that should be queued for persistence |
- Source:
remove(instances)
Queues given instance for deletion.
Parameters:
Name | Type | Description |
---|---|---|
instances |
ResourceInstance | Array.<ResourceInstance> | Instances that should be queued for removing |
- Source:
removeAfterPersistListener(fn)
Removes a after-persist listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
removeAfterRemoveListener(fn)
Removes a after-remove listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
removeBeforePersistListener(fn)
Removes a before-persist listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
removeBeforeRemoveListener(fn)
Removes a before-remove listener.
Parameters:
Name | Type | Description |
---|---|---|
fn |
function | Callback function |
- Source:
removeRelation(relation)
Removes a relation from the store.
Parameters:
Name | Type | Description |
---|---|---|
relation |
ResourceStoreRelation | Relation instance to remove |
- Source: