Syncer
The syncer methods are mixins used in the models and collections to simplify data retrieval from an API. The syncer is setup to work on requests that are started on the client or server. It automatically adds the appropriate API prefixes, and makes the requests. It is modeled after the model sync and collection sync created in Backbone.
The most important thing to note about the syncer methods and attributes is since they are mixed into models and collections, all of them are available on both models and collections.
formatClientUrl Syncer.formatClientUrl(url, api)
This function will create the URL that is expected on the client application. It requires the URL and the api (which defaults to '/api'). This will then create the URL for the API proxy. Generally, formatClientUrl
only needs to be invoked by getUrl
.
getUrl Syncer.getUrl(url, clientPrefix, params)
The method is expecting a url string that is set on a model or collection using the url
or urlRoot
attributes.
interpolateParams Syncer.interpolateParams(modelOrCollection, url, params)
This will interpolate the url strings. For instance if the url you have set on a model is: /game/:category
, and the game has the value dice
for the category
attribute, the resulting url will be /game/dice
.
This function enables us to easily change the url parameters based on attributes in the model / collection. If interpolating a collection, it will check the collection's options for the data to interpolate.
objectsDiffer Syncer.objectsDiffer(modelA, modelB)
This method deeply compares the data of two different models / collections and returns true if they're different, false if they're the same.
sync Syncer.sync(method, model, options)
This method is called automatically behind the scenes when .save
, .fetch
, and .destroy
are called. It sends an AJAX request to an API, either on the client or the server.