object-context.js v1.0.10
JavaScript Object Context
Version: 1.0.2
A JavaScript object change tracking module. This module can be used as a standalone container to track JavaScript objects for changes.
A sandbox demo Angular 1 app that uses the module can be found in this plunker.
Angular 2+
By using the ngDoCheck lifecycle hook in Angular 2+ you can have Angular automatically evaluate the context for changes.
Angular 1.x
To use with Angular 1.x you must include the 'ngObjectContext' module as a dependency and it will automatically evaluate the context for changes.
API Documentation
objectevaluate()This is the change tracking engine. Checks if any of the property values in each of the tracked objects have any changes. Will recursively evaluate child properties that are arrays/objects themselves.
returnsobjectA reference tothisfor chaining.
booleandoesObjectExist(objectReference)Checks to see if the provided object has already been added to the context.
parameterobjectobjectReferenceThe object to test for existence.returnsbooleanif the objects exists already,falseotherwise.
objectadd(obj, isStatusNew)Adds an object to the context. Any changes to the properties on this object will trigger the context to have changes (after
evaluate()is called) and notify any subscribers.This method wraps the passed in object in an object that it can work with. The passed in object will be copied so it can store its original state.
If a meta property does not exist on the object then one will be added to it.
Any changes that are made to this object can be seen by querying the
changesetproperty.parameterobjectobjThe object to start tracking.parameterbooleanisStatusNew[optional]A boolean flag to indicate if this object is to be marked as 'New' or 'Unmodified'. Default value is falsy.returnsobjectA reference tothisfor chaining.
objectdelete(obj, hardDelete, canRemoveMetadata)Marks the provided object as 'Deleted'. If the object doesn't exist, an exception will be thrown.
If
hardDeleteis true, then the object will be instantly removed from the context. Any children of this object will also be removed.Note: When 'hard-deleting' an object, the
_objectMetaproperty is looked at and its status is reverted to its original value before deletion.parameterobjectobjThe object to delete.parameterbooleanhardDelete[optional]A boolean flag to determine if the object should be removed or just marked as 'Deleted'. The default value is falsy.parameterbooleancanRemoveMetadata[optional]A boolean flag to determine if an objects metadata property is to be removed before deleting.returnsobjectA reference tothisfor chaining.throwsErrorif the provided object doesn't exist.
booleanhasChanges(obj)Determines if any of the tracked objects in the context have any active changesets or are marked as 'New', 'Modified', or 'Deleted'. If an object is passed, then just that object will be tested for changes.
parameterobjectobjAn existing context object to test for changes.returnsbooleanTrue if changes exist, false otherwise.
booleanhasChildChanges(obj)Determines if the provided object has any children that are marked as being changed.
parameterobjectobjThe object to check.returnsbooleanTrue if the object has child changes, false otherwise.throwsError if the provided object could not be found.
objectclear(canRemoveMetadata)Removes all currently tracked objects, and resets the state of the context.
This will usually be called when the state of the application is being destroyed, or if any object that are laoded into the context are no longer relevant.
Note: When clearing the context, the
_objectMetaproperty is looked at and its status is reverted to its original value before it is cleared.parameterbooleancanRemoveMetadataWhether or not the objects' metadata property is removed from itself before clear.returnsobjectA reference tothisfor chaining.
arraygetObjects(returnMappedObjects)Returns all objects in the context in their current state.
parameterbooleanreturnMappedObjectsIf true is passed, then the internal objects are returned, otherwise the objects as they were added are returned in their current state.returnsarrayAn array of all existing objects.
arraygetUnmodifiedObjects(parentsOnly)Returns all objects that have status of 'Unmodified'.
parameterbooleanparentsOnlyRetrieve only parent objects if true, false will fetch all objects including children.returnsarrayAn array of objects with a status of 'Unmodified'.
arraygetModifiedObjects(parentsOnly)Returns all objects that have status of 'Modified'.
parameterbooleanparentsOnlyRetrieve only parent objects if true, false will fetch all objects including children.returnsarrayAn array of objects with a status of 'Modified'.
arraygetNewObjects(parentsOnly)Returns all objects that have status of 'New'.
parameterbooleanparentsOnlyRetrieve only parent objects if true, false will fetch all objects including children.returnsarrayAn array of objects with a status of 'New'.
arraygetDeletedObjects(parentsOnly)Returns all objects that have status of 'Deleted'.
parameterbooleanparentsOnlyRetrieve only parent objects if true, false will fetch all objects including children.returnsarrayAn array of objects with a status of 'Deleted'.
arraygetObjectsByType(requestedType)Attempts to find all objects in the context that have the
requestedTypenoted in their metadata. If an object does not provide a type, its default type of 'Object' will be used.parameterstringrequestedTypeThe type of objects to fetch from the context.returnsarrayAn array of objects found.
objectacceptChanges()Applies all changes in currently modified objects. After this, all objects that previously had a status that was not equal to 'Unmodified', will now have an 'Unmodified' status.
If the object has a status of deleted, then the object will be removed from the context.
Objects that were unchanged are not touched.
returnsobjectA reference tothisfor chaining.
arraygetChangeset(obj, includeChildren)Returns the changeset for a specified object. If an object was not provided, then we return the changeset for all objects.
If
includeChildrenis passed along with an object, then we fetch the changesets for all objects in the context, that have the provided object as a parent.parameterobjectobjAn object to search for.parameterbooleanincludeChildren[optional]Include children of the provided (if possible)returnsarrayAn array with the properties that have changed.throwsError if the provided object could not be found.
objectgetOriginal(objectReference)Returns a copy of the original unchanged object in the state that it was in when it was either added or last saved.
If the object is not found then
nullis returned.parameterobjectobjectReferenceThe object to search for.returnsobjectA copy of the original object, or null if not found.
stringgetObjectStatus(obj)Gets an object status for the specified object reference.
parameterobjectobjThe object to search for.returnsstringThe status of the requested object.throwsError ifobjcould not be found.
objectrejectChanges(obj)Rejects changes for an object that exist in the context by setting the values in the object back its original values.
If a single object is passed, it will be tested for existance, and then that one object will be reverted. If no object is passed, then all objects will be reverted.
parameterobjectobj[optional]An existing context object to reject changes for.returnsobjectA reference to this for chaining.throwsError ifobjis provided and could not be found.
numbersubscribeChangeListener(listener)Subcribes the passed listener function that will be invoked when a change has occured.
parameterfunctionlistenerA function to invoke when a change occurs to any objects in the context.returnsnumberThe total number of subscribed listeners.throwsError iflisteneris not afunction.
numberunsubscribeChangeListener(listener)Unsubscribes the provided change listener.
parameterfunctionlistenerA function reference to unsubscribe.returnsnumberThe total number of subscribed listeners.throwsError iflistenerwas not subscribed first.
voidlog()Output the state and all objects in the context to the console.
returnsobjectA reference to this for chaining.
objectcreate()Call this when creating a new object. The object will be flagged as 'New' and will automatically be added to the context.
parameterstringtypeThe type of object to create.parameterobjectobjTHe object structure to add.returnsobjectA reference to this for chaining.
arrayquery(type, params)Used for querying objects from the context by type. Query parameters are optional to filter the data further.
parameterstringtypeThe type of objects to query.parameterobjectparams[optional]An object containing properties and values to search for.returnsarrayAn array of found objects.
Development
- Install karma (with the karma-coverage plugin):
- npm install karma
- npm install karma-coverage
- npm install karma-jasmine
- npm install jasmine-core
- (Windows only) npm install -g karma-cli
- karma init
- karma start