Models allow getting and setting to nested undefined paths. Setting such a path first sets each undefined or null parent to an empty object or empty array. Whether or not a path segment is a number determines whether the implied parent is created as an object or array.
model.set('cars.DeLorean.DMC12.color', 'silver');
// Logs: { cars: { DeLorean: { DMC12: { color: 'silver' }}}}
console.log(model.get());
All model mutators modify data and emit events synchronously. This is only safe to do, because all remote data is synchronized with Operational Transformation, and every client will eventually see a consistent view of the same data. With a more naive approach to syncing data to the server and other clients, updates to the data would need to wait until they were confirmed successful from the server.
As well as a synchronous interface, model mutators have an optional callback with an error argument callback(err)
. This callback is called when the operation is confirmed from the server, which may be desired to confirm that data was saved before updating the UI in some rare cases. This callback should be used very rarely in practice, and data updates should be treated as sychronous, so that the UI responds immediately even if a user has a high latency connection or is currently disconnected.
previous = model.set(path, value, [callback])
path
Model path to setvalue
Value to assignprevious
Returns the value that was set at the path previouslycallback
(optional) Invoked upon completion of a successful or failed operation
obj = model.del(path, [callback])
path
Model path of object to deleteobj
Returns the deleted object
obj = model.setNull(path, value, [callback])
path
Model path to setvalue
Value to assign only if the path is null or undefinedobj
Returns the object at the path if it is not null or undefined. Otherwise, returns the new value
previous = model.setDiff(path, value, [callback])
path
Model path to setvalue
Value to be set if not strictly equal to the current valueprevious
Returns the value that was set at the path previously
model.setDiffDeep(path, value, [callback])
model.setArrayDiff(path, value, [callback])
model.setArrayDiffDeep(path, value, [callback])
path
Model path to setvalue
Value to be set if different. Deep methods will do a deep traversal and deep equality check. Array methods should be used when diffing two different arrays and only inserts, removes, and moves at the top level are desired.setDiffDeep
can diff arrays as well but may produce a set on a particular property within an array. These methods may end up performing zero or many mutations.
id = model.add(path, object, [callback])
path
Model path to setobject
A document to add. If the document has anid
property, it will be set at that value underneath the path. Otherwise, an id frommodel.id()
will be set on the object firstid
Returnsobject.id
model.setEach(path, object, [callback])
path
Model path underneath which each property will be setobject
An object whose properties are each set individually
number = model.increment(path, [byNumber], [callback])
path
Model path to setbyNumber
(optional) Number specifying amount to increment or decrement if negative. Defaults to 1number
Returns the new value that was set after incrementing
Array methods can only be used on paths set to arrays, null, or undefined. If the path is null or undefined, the path will first be set to an empty array before applying the method.
length = model.push(path, value, [callback])
path
Model path to an arrayvalue
An item to add to the end of the arraylength
Returns the length of the array with the new item added
length = model.unshift(path, value, [callback])
path
Model path to an arrayvalue
An item to add to the beginning of the arraylength
Returns the length of the array with the new item added
length = model.insert(path, index, values, [callback])
path
Model path to an arrayindex
Index at which to start inserting. This can also be specified by appending it to the path instead of as a separate argumentvalues
An array of items to insert at the indexlength
Returns the length of the array with the new items added
item = model.pop(path, [callback])
path
Model path to an arrayitem
Removes the last item in the array and returns it
item = model.shift(path, [callback])
path
Model path to an arrayitem
Removes the first item in the array and returns it
removed = model.remove(path, index, [howMany], [callback])
path
Model path to an arrayindex
Index at which to start removing itemshowMany
(optional) Number of items to remove. Defaults to 1removed
Returns an array of removed items
moved = model.move(path, from, to, [howMany], [callback])
path
Model path to an arrayfrom
Starting index of the item to moveto
New index where the item should be movedhowMany
(optional) Number of items to move. Defaults to 1moved
Returns an arry of items that were moved
String methods can only be used on paths set to strings, null, or undefined. If the path is null or undefined, the path will first be set to an empty string before applying the method.
The string methods support collaborative text editing, and Derby uses string methods to bind changes to all text inputs and textareas by default.
model.stringInsert(path, index, text, [callback])
path
Model path to a stringindex
Character index within the string at which to inserttext
String to insert
Next ➔
model.stringRemove(path, index, howMany, [callback])
path
Model path to a stringindex
Starting character index of the string at which to removehowMany
Number of characters to remove