Updating collections

Often an integral part of the delta processing is modifying a collection stored in JS, e.g. in an array. This is specially true if you use a MV* framework like AngularJS.

For example, you may store the initially received elements in an array and later modify it as deltas come from Snorky.

In order to leverage this pattern there is the Snorky.DataSync.CollectionDeltaProcessor() class.

class Snorky.DataSync.CollectionDeltaProcessor(collections, options)

Updates one or more collections with deltas received from Snorky.

  • collections (Object) – A dictionary where each key is a model class name and each value is a collection.
  • options (Object) –

    An optional dictionary with additional options.


    Provides a custom item comparison (see below).

itemsAreEqual(item, other, delta)

If return true, two items from a collection will be considered the same. This function is used for processing update and deletion deltas.

The updated or deleted element will be the one that makes this function returns true when compared with the element in the delta.

By default compares the id field in both item and other and returns true if they are equal.


Checks if the delta is associated with any registered collection. If it is, updates the collection adding, updating or removing the matching element.


A collection, as understood by Snorky.DataSync.CollectionDeltaProcessor() is a class which allows inserting methods and getting an iterator with update and delete capabilities.

class Snorky.DataSync.Collection()

An abstract interface for a collection.


Inserts a new element in the collection.


Returns an iterator to the collection.

class Snorky.DataSync.Iterator()

An abstract interface for an iterator.


Advances the iterator to the next element and returns it.

This method should also be called to retrieve the first element in the collection.


True if there are elements in the collection which the iterator has not explored.


Remove the last element returned by next() from the collection.


Replace the last element returned by next() with the value provided as argument.

Array collection

The most common collection is the one that is backend by a JavaScript array.

class Snorky.DataSync.ArrayCollection(array, options)
  • array (Array) – An array, to which this class will expose a collection interface.
  • options (Object) –

    An optional dictionary of options:


    When an element is inserted or updated, this function will be called with the element to insert or update, and the object returned will be inserted or updated instead.

    This is often used when you use fat models, that is, you extend the JSON objects that you receive from the server in order to provide helper methods that calculate additional data or perform special operations.

    This function gives you the opportunity to add additional methods or perform transformations in the models received from Snorky.

Single item collection

Sometimes the data you synchronize with Snorky is not a list but a single element. Snorky.DataSync.SingleItemCollection() allows you to update it by providing an update callback.

class Snorky.DataSync.SingleItemCollection(readHandler, updateHandler, removeHandler)

Virtual collection of a single item.

  • readHandler (function) – Called to get the current item value.
  • updateHandler (function) – Called to update the item value.
  • removeHandler (function) – Optional, called when the item is deleted.

Example usage

The following code would update the array comments with the deltas received from Snorky.

var collectionProcessor = new Snorky.DataSync.CollectionDeltaProcessor({
  "Comment": new Snorky.DataSync.ArrayCollection(comments)

// Delegate delta processing to the collection processor
snorky.services.datasync.deltaReceived.add(function(delta) {