Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Showing with 23 additions and 59 deletions.
  1. +14 −2 README.md
  2. +9 −57 src/backpusher.js
View
16 README.md
@@ -1,6 +1,18 @@
-## Backpusher
+## Backpusher 0.0.2
-For details, please read the [article on our blog](http://blog.pusher.com/2011/6/21/backbone-js-now-realtime-with-pusher) about this library.
+## What is this?
+
+Backpusher is a library for using Pusher to sync your Backbone models between clients, the server and other clients. For details, please read the [article on our blog](http://blog.pusher.com/2011/6/21/backbone-js-now-realtime-with-pusher).
+
+## Breaking change in version 0.0.2
+
+### The change
+
+When Backpusher syncs data via `Backbone.sync()`, it sends the `socket_id` of the client connection that initiated the sync. In version 0.0.1, this data was sent as a URL parameter. In version 0.0.2, this data is sent as a header.
+
+### Updating your code
+
+Find any pieces of code that use the `socket_id` sent by a `Backbone.sync()` request. Update these pieces of code to retrieve the `socket_id` from the headers of the request, rather than the URL.
## Requirements
View
66 src/backpusher.js
@@ -1,5 +1,5 @@
-// Backpusher.js 0.0.1
-// (c) 2011 Pusher.
+// Backpusher.js 0.0.2
+// (c) 2011-2012 Pusher.
// Backpusher may be freely distributed under the MIT license.
// For all details and documentation:
// http://github.com/pusher/backpusher
@@ -92,63 +92,15 @@
}
};
- // Map from CRUD to HTTP for our default `Backbone.sync` implementation.
- var methodMap = {
- 'create': 'POST',
- 'update': 'PUT',
- 'delete': 'DELETE',
- 'read' : 'GET'
- };
-
+ // Add socket ID to every Backbone.sync request
+ var origBackboneSync = Backbone.sync;
Backbone.sync = function(method, model, options) {
- var type = methodMap[method];
-
- // Default JSON-request options.
- var params = _.extend({
- type: type,
- dataType: 'json'
- }, options);
-
- if (!(model && model.url)) {
- throw new Error("A 'url' property or function must be specified");
- }
-
- if (!params.url) {
- params.url = _.isFunction(model.url) ? model.url() : model.url;
- params.url += '?socket_id=' + Backbone.pusher_socket_id;
- }
-
- // Ensure that we have the appropriate request data.
- if (!params.data && model && (method == 'create' || method == 'update')) {
- params.contentType = 'application/json';
- params.data = JSON.stringify(model.toJSON());
- }
-
- // For older servers, emulate JSON by encoding the request into an HTML-form.
- if (Backbone.emulateJSON) {
- params.contentType = 'application/x-www-form-urlencoded';
- params.data = params.data ? {model : params.data} : {};
- }
-
- // For older servers, emulate HTTP by mimicking the HTTP method with `_method`
- // And an `X-HTTP-Method-Override` header.
- if (Backbone.emulateHTTP) {
- if (type === 'PUT' || type === 'DELETE') {
- if (Backbone.emulateJSON) params.data._method = type;
- params.type = 'POST';
- params.beforeSend = function(xhr) {
- xhr.setRequestHeader('X-HTTP-Method-Override', type);
- };
- }
- }
-
- // Don't process data on a non-GET request.
- if (params.type !== 'GET' && !Backbone.emulateJSON) {
- params.processData = false;
- }
+ options.headers = _.extend(
+ { 'X-Pusher-Socket-ID': Backbone.pusher_socket_id },
+ options.headers
+ );
- // Make the request.
- return $.ajax(params);
+ return origBackboneSync(method, model, options);
};
// Export:

No commit comments for this range

Something went wrong with that request. Please try again.