ResourceStoreRelation

ResourceStoreRelation

new ResourceStoreRelation(store, relatedStore, fkAttr, onUpdate, onRemove, onDispose)

Constructor class for a relation between two stores.

Parameters:
Name Type Description
store ResourceStore

Store to create the relation on

relatedStore ResourceStore

Related store

fkAttr String

Name of foreign key attribute on instances on related store

onUpdate function | "update" | "null" | "ignore"

What to do on the related instances if instances on the store are updated

onRemove function | "forget" | "null" | "ignore"

What to do on the related instances if instances on the store are removed

onDispose function | "forget" | "null" | "ignore"

What to do on the related instances if instances on the store are disposed

Source:
Example
// Basic usage of a ResourceStoreRelation
inject(function (ResourceFactoryService, $q) {
    var
        service = ResourceFactoryService('TestResourceService', 'http://test/:pk/'),
        relatedService = ResourceFactoryService('RelatedTestResourceService', 'http://relatedtest/:pk/'),
        instance1 = service.new(),
        instance2 = service.new(),
        relatedInstance1 = relatedService.new({fk: instance1.pk}),
        relatedInstance2 = relatedService.new({fk: instance2.pk}),
        store = service.createStore([instance1, instance2]),
        relatedStore = relatedService.createStore([relatedInstance1, relatedInstance2]);

    $httpBackend.expect('POST', 'http://test/').respond(201, {pk: 1});
    $httpBackend.expect('POST', 'http://test/').respond(201, {pk: 2});

    store.createRelation({
        relatedStore: relatedStore,
        fkAttr: 'fk'
    });

    $q.when()
        .then(function () {
            expect(relatedInstance1.fk).toBe(-1);
            expect(relatedInstance2.fk).toBe(-2);
        })
        .then(function () {
            store.persist(instance1);
            store.persist(instance2);
        })
        .then(function () {
            return store.execute();
        })
        .then(function () {
            expect(relatedInstance1.fk).toBe(1);
            expect(relatedInstance2.fk).toBe(2);
        })
        .then(done);

    $httpBackend.flush();
});

Methods

getFkAttr() → {String}

Gets the FK attribute name.

Source:
Returns:

Name of the foreign key attribute on related instances

Type
String

getRelatedStore() → {ResourceStore}

Gets the store the configured store is related on.

Source:
Returns:

Related resource store

Type
ResourceStore

getStore() → {ResourceStore}

Gets the store the relation is configured on.

Source:
Returns:

Resource store to work on

Type
ResourceStore

handleDispose(pkValue)

Starts the configured dispose behaviour on the referencing instances on the related store. This is
called when an instance was removed or forgot on the store.

Parameters:
Name Type Description
pkValue String | int

PK of the instance on the store

Source:

handleRemove(pkValue)

Starts the configured remove behaviour on the referencing instances on the related store. This is
called when an instance was deleted on the server.

Parameters:
Name Type Description
pkValue String | int

PK of the instance on the store

Source:

handleUpdate(oldPkValue, newPkValue)

Starts the configured update behaviour on the referencing instances on the related store. This is
called when an instance was saved / updated on the server and got a new PK.

Parameters:
Name Type Description
oldPkValue String | int

PK of the instance on the store before the update

newPkValue String | int

PK of the instance on the store after the update

Source: