new ResourcePhantomIdFactory(generateFn, isPhantomFn)
Constructor function for a phantom id generate. Takes a function that generates the PK, and a
functions that checks if the given PK is a phantom PK.
Parameters:
Name | Type | Description |
---|---|---|
generateFn |
function | Function that returns a new, phantom ID. Gets the instance as the first parameter |
isPhantomFn |
function | Function that checks if the first given parameter is a phantom ID. Gets the instance as the second parameter |
- Source:
- See:
Example
// ResourcePhantomIdFactory that creates negative IDs as phantom IDs (NOTE: this is already a build-in
// phantom ID factory, so you do not have to implement this)
inject(function (ResourceFactoryService, ResourcePhantomIdFactoryService) {
var
lastPkValue = 0,
generator = ResourcePhantomIdFactoryService.createPhantomIdFactory({
generate: function () {
return --lastPkValue;
},
is: function (pkValue) {
return pkValue < 0;
}
}),
service = ResourceFactoryService('TestResourceService', 'http://test/:pk/', {
phantomIdGenerator: generator
}),
phantomInstance1 = service.new(),
phantomInstance2 = service.new();
expect(phantomInstance1.$isPhantom()).toBe(true);
expect(phantomInstance2.$isPhantom()).toBe(true);
// Change IDs to non-negative numbers
phantomInstance1.pk = 1;
phantomInstance2.pk = 2;
expect(phantomInstance1.$isPhantom()).toBe(false);
expect(phantomInstance2.$isPhantom()).toBe(false);
});
Methods
generate(instance) → {String|int}
Generates a new phantom PK value
Parameters:
Name | Type | Description |
---|---|---|
instance |
ResourceInstance | Instance to generate the phantom ID for |
Returns:
- Type
- String | int
isPhantom(pkValue, instance) → {*}
Checks if the given PK value on the given instance is a phantom PK value
Parameters:
Name | Type | Description |
---|---|---|
pkValue |
String | int | ID value to check |
instance |
ResourceInstance | Instance to work on |
Returns:
- Type
- *