-
Notifications
You must be signed in to change notification settings - Fork 1
Reference
General behavior
- All non-querying methods return the object on which they were called.
- All querying methods return data node(s) wrapped in a Flock datastore object.
- Scope for all methods is the current node and its sub-tree.
Creates a Flock datastore object out of a data node. May take optional flags that turn off features for performance gain.
-
root: Any JavaScript object that we want to further query, modify, or receive events of modifications within. -
options: A set of flags controlling the flock's behavior.-
noevent: No events. Turns off methods.on,.off,.delegate,.once, and.trigger. -
nomulti: No complex queries, only single nodes may be accessed. Turns off method.query. -
nochaining: No wrapping of querying methods in Flock object. Methods.get,.parent,.closest, and.querywill return raw data instead.
-
Special value for options: flock.COMPAT, compatible with flock 1.6, and is equal to flags noevent and nochaining being turned on.
var ds = flock({
employees: {
Green: {
John: {}
},
Smith: {
John: {}
Matt: {}
}
}
});
Datastore node object.
Read-only property.
ds
.get('employees.Smith.John')
.root; // {...}
Rertieves a single node from the specified path.
-
path: Datastore path relative to current node. May be in string or array notation. See Path for details on path format. -
nochaining: Flag whentrue, will return datastore node instead of flock object. Affects the current call only. May be set globally on initializing the mainflockobject. See above.
// retrieves node for John Smith
var result = ds.get('employees.Smith.John');
Retrieves all nodes matching the path expression. Not available when nomulti is on.
-
path: Datastore path relative to current node. May be in string or array notation. See Path for details on path format. -
options:-
limit: Maximum number of entries to retrieve. Default unlimited (undefined). -
mode: What to do or return upon querying. See below for possible values. Default:flock.VALUES. -
loopback: Whether to traverse loopbacks. Default:false. -
undef: Whether to collect undefined entries. Default:false. -
value: Value to set or callback function the result of which to set on each affected node. When left undefined,.queryreturns collected nodes.
-
Values for mode:
-
flock.KEYS: Only keys of traversed nodes are collected in an array. The same key may be included multiple times. -
flock.VALUES: Only values of traversed nodes are collected in an array. -
flock.BOTH: Both keys and values are collected. Keys are unique. Values with the same key may overwrite each other. -
flock.DEL: Deletes affected nodes. -
flock.COUNT: Counts affected nodes.
// returns nodes for all employees with first name 'John'
var result = ds.query('employees.*.John'); // flock([{...}, {...}, {...}, ...])
Maps immediate child nodes to fit structure defined by the values on the provided relative paths. Should be used on nodes with uniform children, such as arrays.
Each argument is a path in either string or array notation.
Last argument specifies leaf nodes. If it's an empty path, original child nodes will be used as leaf nodes in the output.
Doesn't check uniqueness. Equivalent leaf nodes will overwrite each other.
The following code creates a lookup by employees' department and last name. Notice how the two Smiths overwrite one another.
var ds = flock({
employees: {
emp1: {fname: "John", lname: "Smith", department: "IT"},
emp2: {fname: "John", lname: "Green", department: "HR"},
emp3: {fname: "Matt", lname: "Smith", department: "IT"},
}
});
ds
.get('employees')
.map(['department'], ['lname'], ['fname']);
/* flock({
IT: {Smith: "Matt"},
HR: {Green: "John"}
}) */
General behavior
- When option
noeventis on, modification methods don't fire events.
Stores a single value on the specified path.
-
path: Datastore path relative to current node. May be in string or array notation. See Path for details on path format. -
value: Any variable. Whenvalueis object, its contents become queryable. -
options:-
data: Custom data to be passed to event handler. -
trigger: Whether to trigger event on change. Default:true.
-
Fires flock.CHANGE event if the affected node was previously set, and also flock.ADD otherwise.
ds
.get('employees')
.set('Smith.John', {height: 5.7});
Removes node from datastore at the specified path.
-
path: Datastore path relative to current node. May be in string or array notation. See Path for details on path format. -
options:-
data: Custom data to be passed to event handler. -
trigger: Whether to trigger event on change. Default:true.
-
Fires flock.REMOVE event on successful removal.
ds
.get('employees')
.unset('Smith.John');
Same as .unset() but removes empty parent nodes as well.
Fires flock.REMOVE event on successful removal.
In the example below, the node employees.Green, which would have been left empty using .unset, is removed as well.
ds.cleanup('employees.Green.John'); // {employees: Smith: {John: {}, Matt: {}}}
Subscribes to event on the current node.
-
path: Datastore path to be observed. -
eventName: Name of expected event. -
handler: Event handler function. Arguments:-
event: Object containingtargetdatastore node andnameevent name. -
data: Custom data that was passed along to the event trigger.
-
Subscribes to all changes in the 'employee' branch of the datastore.
ds
.get('employees')
.on('changed', function () { alert("boo!"); });
Same as .on except event is captured only once. After being captured, automatically unsubscribes from event.
Delegates event capturing to a path relative to current node.
-
pPath: Node to delegate capturing to. May be in string or array notation. When optionnomultiis not set, pPath may be a query expression. See Path for details on path format.
For the rest of the arguments, see .on.
Subscribes to all changes affecting the node 'employees.Smith'.
ds
.delegate('employees', 'change', 'Smith', function () { alert("boo!"); });
Unsubscribes from an event.
-
path: Datastore path that was observed. -
eventName: Event name. -
handler: Event handler function. Optional. When argumenthandleris omitted, all handlers pertaining eventeventNamewill be removed from the current node, including delegates.
Node 'employees' no longer captures events.
ds
.off('employees', 'change');
Triggers event on the specified path.
-
path: Datastore path to trigger the event on. -
eventName: Event name. -
options:-
data: Custom data to be passed to event handlers. -
target: Custom event target.
-
ds
.get('employees.Smith.John')
.trigger('myEvent', {foo: "bar"});