Created
March 20, 2012 16:31
-
-
Save eldavido/2137955 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# backbone_sync.coffee | |
# Defines a synchronization strategy that works with the Google Gadgets container | |
# options hash: | |
# "manualOverride" defines a callback that takes the "obj" parameters from | |
# makeRequest; intended for manually inspecting/parsing the service response. | |
# "success" and "error" are like their Backbone.js equivalents. | |
# "nocache" if truthy, ignore the value in makeRequest() cache. | |
Backbone.sync = (method, model, options) -> | |
syncParams = {} | |
syncParams[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON | |
syncParams[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH | |
syncParams[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = oauthServiceName | |
url = if _.isFunction(model.url) then model.url() else model.url | |
switch method | |
when 'read' | |
if options.nocache | |
Backbone.makeRequestNocache(url, ((o) => Backbone.syncCallback(o, model, options, url)), | |
syncParams, options.refreshInterval) | |
else | |
gadgets.io.makeRequest(url, ((o) => Backbone.syncCallback(o, model, options, url)), syncParams) | |
when 'create' | |
syncParams[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST | |
syncParams[gadgets.io.RequestParameters.POST_DATA] = JSON.stringify model.toJSON() | |
syncParams[gadgets.io.RequestParameters.HEADERS] = { "Content-Type": "application/json" } | |
gadgets.io.makeRequest(url, ((o) => Backbone.syncCallback(o, model, options, url)), syncParams) | |
when 'update' | |
syncParams[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.PUT | |
syncParams[gadgets.io.RequestParameters.POST_DATA] = JSON.stringify model.toJSON() | |
syncParams[gadgets.io.RequestParameters.HEADERS] = { "Content-Type": "application/json" } | |
gadgets.io.makeRequest(url, ((o) => Backbone.syncCallback(o, model, options, url)), syncParams) | |
when 'delete' | |
syncParams[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.DELETE | |
gadgets.io.makeRequest(url, ((o) => Backbone.syncCallback(o, model, options, url)), syncParams) | |
# called when we get data/error from the server. | |
Backbone.syncCallback = (returnObj, model, syncOpts, url) -> | |
# Always log error event if we get a 500-series error | |
if(returnObj.rc >= 500 and returnObj.rc <= 599) | |
window.router.recordGadgetEvent(_type: 'GadgetServerError', rc: returnObj.rc, url: url) | |
if syncOpts.manualOverride | |
syncOpts.manualOverride(returnObj) | |
else | |
if returnObj.errors.length == 0 and (returnObj.rc >= 200 and returnObj.rc <= 200) | |
syncOpts.success(returnObj.data, returnObj.rc, returnObj) | |
else | |
syncOpts.error(returnObj) | |
Backbone.makeRequestNocache = (url, callback, params, refreshInterval) -> | |
sep = "?" | |
if (url.indexOf("?") > -1) | |
sep = "&" | |
url = [ url, sep, "noCache=", new Date().getTime() ].join("") | |
gadgets.io.makeRequest(url, callback, params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment