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])
pathModel path to setvalueValue to assignpreviousReturns the value that was set at the path previouslycallback(optional) Invoked upon completion of a successful or failed operation
obj = model.del(path, [callback])
pathModel path of object to deleteobjReturns the deleted object
obj = model.setNull(path, value, [callback])
pathModel path to setvalueValue to assign only if the path is null or undefinedobjReturns the object at the path if it is not null or undefined. Otherwise, returns the new value
previous = model.setDiff(path, value, [callback])
pathModel path to setvalueValue to be set if not strictly equal to the current valuepreviousReturns the value that was set at the path previously
model.setDiffDeep(path, value, [callback])model.setArrayDiff(path, value, [callback])model.setArrayDiffDeep(path, value, [callback])
pathModel path to setvalueValue 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.setDiffDeepcan 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])
pathModel path to setobjectA document to add. If the document has anidproperty, it will be set at that value underneath the path. Otherwise, an id frommodel.id()will be set on the object firstidReturnsobject.id
model.setEach(path, object, [callback])
pathModel path underneath which each property will be setobjectAn object whose properties are each set individually
number = model.increment(path, [byNumber], [callback])
pathModel path to setbyNumber(optional) Number specifying amount to increment or decrement if negative. Defaults to 1numberReturns 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])
pathModel path to an arrayvalueAn item to add to the end of the arraylengthReturns the length of the array with the new item added
length = model.unshift(path, value, [callback])
pathModel path to an arrayvalueAn item to add to the beginning of the arraylengthReturns the length of the array with the new item added
length = model.insert(path, index, values, [callback])
pathModel path to an arrayindexIndex at which to start inserting. This can also be specified by appending it to the path instead of as a separate argumentvaluesAn array of items to insert at the indexlengthReturns the length of the array with the new items added
item = model.pop(path, [callback])
pathModel path to an arrayitemRemoves the last item in the array and returns it
item = model.shift(path, [callback])
pathModel path to an arrayitemRemoves the first item in the array and returns it
removed = model.remove(path, index, [howMany], [callback])
pathModel path to an arrayindexIndex at which to start removing itemshowMany(optional) Number of items to remove. Defaults to 1removedReturns an array of removed items
moved = model.move(path, from, to, [howMany], [callback])
pathModel path to an arrayfromStarting index of the item to movetoNew index where the item should be movedhowMany(optional) Number of items to move. Defaults to 1movedReturns 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])
pathModel path to a stringindexCharacter index within the string at which to inserttextString to insert
Next ➔
model.stringRemove(path, index, howMany, [callback])
pathModel path to a stringindexStarting character index of the string at which to removehowManyNumber of characters to remove