Skip to content

Instantly share code, notes, and snippets.

@KellyRice
Created March 28, 2012 16:09
Show Gist options
  • Save KellyRice/2227835 to your computer and use it in GitHub Desktop.
Save KellyRice/2227835 to your computer and use it in GitHub Desktop.
Dave code 1
// We use a separate function rather than defining this
// twice: once for the Model, and once for the Collection
kinveyAuthenticatedSync = (method, model, options) ->
// Before we send the request ...
options.beforeSend = (jqXHR) ->
// Add the Basic Authentication header
jqXHR.setRequestHeader(
'Authorization',
'Basic ' + $.base64.encode(
kinvey_app_key + ':' + kinvey_master_secret
)
)
// Now that we added the Basic Authentication header,
// call the default Backbone sync method
Backbone.sync.call(this, method, model, options)
// All models stored on the Kinvey backend will subclass from
// KinveyModel rather than Backbone.Model
class KinveyModel extends Backbone.Model
sync: kinveyAuthenticatedSync
idAttribute: '_id'
// Same for collections - they should subclass from KinveyCollection
class KinveyCollection extends Backbone.Collection
sync: kinveyAuthenticatedSync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment