Class: DynamicView

DynamicView(collection, name, optionsopt)

new DynamicView(collection, name, optionsopt)

DynamicView class is a versatile 'live' view class which can have filters and sorts applied. Collection.addDynamicView(name) instantiates this DynamicView object and notifies it whenever documents are add/updated/removed so it can remain up-to-date. (chainable)

Parameters:
Name Type Attributes Description
collection Collection

A reference to the collection to work against

name string

The name of this dynamic view

options object <optional>

(Optional) Pass in object with 'persistent' and/or 'sortPriority' options.

Properties
Name Type Attributes Default Description
persistent boolean <optional>
false

indicates if view is to main internal results array in 'resultdata'

sortPriority string <optional>
'passive'

'passive' (sorts performed on call to data) or 'active' (after updates)

minRebuildInterval number

minimum rebuild interval (need clarification to docs here)

Implements:
Source:
See:
Example
var mydv = mycollection.addDynamicView('test');  // default is non-persistent
mydv.applyFind({ 'doors' : 4 });
mydv.applyWhere(function(obj) { return obj.name === 'Toyota'; });
var results = mydv.data();

Methods

applyFilter(filter) → {DynamicView}

applyFilter() - Adds or updates a filter in the DynamicView filter pipeline

Parameters:
Name Type Description
filter object

A filter object to add to the pipeline. The object is in the format { 'type': filter_type, 'val', filter_param, 'uid', optional_filter_id }

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView

applyFind(query, uidopt) → {DynamicView}

applyFind() - Adds or updates a mongo-style query option in the DynamicView filter pipeline

Parameters:
Name Type Attributes Description
query object

A mongo-style query object to apply to pipeline

uid string | number <optional>

Optional: The unique ID of this filter, to reference it in the future.

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView

applySimpleSort(propname, options) → {DynamicView}

applySimpleSort() - Used to specify a property used for view translation.

Parameters:
Name Type Description
propname string

Name of property by which to sort.

options object | boolean

boolean for sort descending or options object

Properties
Name Type Attributes Default Description
desc boolean <optional>
false

whether we should sort descending.

disableIndexIntersect boolean <optional>
false

whether we should explicity not use array intersection.

forceIndexIntersect boolean <optional>
false

force array intersection (if binary index exists).

useJavascriptSorting boolean <optional>
false

whether results are sorted via basic javascript sort.

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView
Example
dv.applySimpleSort("name");

applySort(comparefun) → {DynamicView}

applySort() - Used to apply a sort to the dynamic view

Parameters:
Name Type Description
comparefun function

a javascript compare function used for sorting

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView
Example
dv.applySort(function(obj1, obj2) {
  if (obj1.name === obj2.name) return 0;
  if (obj1.name > obj2.name) return 1;
  if (obj1.name < obj2.name) return -1;
});

applySortCriteria(properties) → {DynamicView}

applySortCriteria() - Allows sorting a resultset based on multiple columns.

Parameters:
Name Type Description
properties array

array of property names or subarray of [propertyname, isdesc] used evaluate sort order

Source:
Returns:

Reference to this DynamicView, sorted, for future chain operations.

Type
DynamicView
Example
// to sort by age and then name (both ascending)
dv.applySortCriteria(['age', 'name']);
// to sort by age (ascending) and then by name (descending)
dv.applySortCriteria(['age', ['name', true]);
// to sort by age (descending) and then by name (descending)
dv.applySortCriteria(['age', true], ['name', true]);

applyWhere(fun, uidopt) → {DynamicView}

applyWhere() - Adds or updates a javascript filter function in the DynamicView filter pipeline

Parameters:
Name Type Attributes Description
fun function

A javascript filter function to apply to pipeline

uid string | number <optional>

Optional: The unique ID of this filter, to reference it in the future.

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView

branchResultset(transform, parametersopt) → {Resultset}

branchResultset() - Makes a copy of the internal resultset for branched queries. Unlike this dynamic view, the branched resultset will not be 'live' updated, so your branched query should be immediately resolved and not held for future evaluation.

Parameters:
Name Type Attributes Description
transform string | array

Optional name of collection transform, or an array of transform steps

parameters object <optional>

optional parameters (if optional transform requires them)

Source:
Returns:

A copy of the internal resultset for branched queries.

Type
Resultset
Example
var db = new loki('test');
var coll = db.addCollection('mydocs');
var dv = coll.addDynamicView('myview');
var tx = [
  {
    type: 'offset',
    value: '[%lktxp]pageStart'
  },
  {
    type: 'limit',
    value: '[%lktxp]pageSize'
  }
];
coll.addTransform('viewPaging', tx);

// add some records

var results = dv.branchResultset('viewPaging', { pageStart: 10, pageSize: 10 }).data();     

count() → {number}

count() - returns the number of documents representing the current DynamicView contents.

Source:
Returns:

The number of documents representing the current DynamicView contents.

Type
number

data(optionsopt) → {array}

data() - resolves and pending filtering and sorting, then returns document array as result.

Parameters:
Name Type Attributes Description
options object <optional>

optional parameters to pass to resultset.data() if non-persistent

Properties
Name Type Description
forceClones boolean

Allows forcing the return of cloned objects even when the collection is not configured for clone object.

forceCloneMethod string

Allows overriding the default or collection specified cloning method. Possible values include 'parse-stringify', 'jquery-extend-deep', 'shallow', 'shallow-assign'

removeMeta bool

Will force clones and strip $loki and meta properties from documents

Source:
Returns:

An array of documents representing the current DynamicView contents.

Type
array

mapReduce(mapFunction, reduceFunction)

mapReduce() - data transformation via user supplied functions

Parameters:
Name Type Description
mapFunction function

this function accepts a single document for you to transform and return

reduceFunction function

this function accepts many (array of map outputs) and returns single value

Source:
Returns:

The output of your reduceFunction

rematerialize(optionsopt) → {DynamicView}

rematerialize() - internally used immediately after deserialization (loading) This will clear out and reapply filterPipeline ops, recreating the view. Since where filters do not persist correctly, this method allows restoring the view to state where user can re-apply those where filters.

Parameters:
Name Type Attributes Description
options Object <optional>

(Optional) allows specification of 'removeWhereFilters' option

Source:
Fires:
  • DynamicView.event:rebuild
Returns:

This dynamic view for further chained ops.

Type
DynamicView

removeFilter(uid) → {DynamicView}

removeFilter() - Remove the specified filter from the DynamicView filter pipeline

Parameters:
Name Type Description
uid string | number

The unique ID of the filter to be removed.

Source:
Returns:

this DynamicView object, for further chain ops.

Type
DynamicView

removeFilters(optionsopt)

removeFilters() - Used to clear pipeline and reset dynamic view to initial state. Existing options should be retained.

Parameters:
Name Type Attributes Description
options object <optional>

configure removeFilter behavior

Properties
Name Type Attributes Description
queueSortPhase boolean <optional>

(default: false) if true we will async rebuild view (maybe set default to true in future?)

Source: