Update vendor/backbone.

Former-commit-id: dc1a37f3c44eecc1fcd1cba2018d70e8c337d6f3
This commit is contained in:
John-David Dalton
2012-11-04 14:40:23 -08:00
parent 218e3b66f2
commit 6c8e19a321
5 changed files with 72 additions and 22 deletions

View File

@@ -185,16 +185,16 @@
var defaults; var defaults;
var attrs = attributes || {}; var attrs = attributes || {};
if (options && options.collection) this.collection = options.collection; if (options && options.collection) this.collection = options.collection;
if (options && options.parse) attrs = this.parse(attrs);
if (defaults = _.result(this, 'defaults')) {
attrs = _.extend({}, defaults, attrs);
}
this.attributes = {}; this.attributes = {};
this._escapedAttributes = {}; this._escapedAttributes = {};
this.cid = _.uniqueId('c'); this.cid = _.uniqueId('c');
this.changed = {}; this.changed = {};
this._changes = {}; this._changes = {};
this._pending = {}; this._pending = {};
if (options && options.parse) attrs = this.parse(attrs);
if (defaults = _.result(this, 'defaults')) {
attrs = _.extend({}, defaults, attrs);
}
this.set(attrs, {silent: true}); this.set(attrs, {silent: true});
// Reset change tracking. // Reset change tracking.
this.changed = {}; this.changed = {};
@@ -989,7 +989,7 @@
}; };
// Cached regex for cleaning leading hashes and slashes. // Cached regex for cleaning leading hashes and slashes.
var routeStripper = /^[#\/]/; var routeStripper = /^[#\/]|\s+$/;
// Cached regex for stripping leading and trailing slashes. // Cached regex for stripping leading and trailing slashes.
var rootStripper = /^\/+|\/+$/g; var rootStripper = /^\/+|\/+$/g;
@@ -1365,7 +1365,10 @@
var type = methodMap[method]; var type = methodMap[method];
// Default options, unless specified. // Default options, unless specified.
options || (options = {}); _.defaults(options || (options = {}), {
emulateHTTP: Backbone.emulateHTTP,
emulateJSON: Backbone.emulateJSON
});
// Default JSON-request options. // Default JSON-request options.
var params = {type: type, dataType: 'json'}; var params = {type: type, dataType: 'json'};
@@ -1382,16 +1385,16 @@
} }
// For older servers, emulate JSON by encoding the request into an HTML-form. // For older servers, emulate JSON by encoding the request into an HTML-form.
if (Backbone.emulateJSON) { if (options.emulateJSON) {
params.contentType = 'application/x-www-form-urlencoded'; params.contentType = 'application/x-www-form-urlencoded';
params.data = params.data ? {model: params.data} : {}; params.data = params.data ? {model: params.data} : {};
} }
// For older servers, emulate HTTP by mimicking the HTTP method with `_method` // For older servers, emulate HTTP by mimicking the HTTP method with `_method`
// And an `X-HTTP-Method-Override` header. // And an `X-HTTP-Method-Override` header.
if (Backbone.emulateHTTP && (type === 'PUT' || type === 'DELETE')) { if (options.emulateHTTP && (type === 'PUT' || type === 'DELETE')) {
params.type = 'POST'; params.type = 'POST';
if (Backbone.emulateJSON) params.data._method = type; if (options.emulateJSON) params.data._method = type;
var beforeSend = options.beforeSend; var beforeSend = options.beforeSend;
options.beforeSend = function(xhr) { options.beforeSend = function(xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', type); xhr.setRequestHeader('X-HTTP-Method-Override', type);
@@ -1400,7 +1403,7 @@
} }
// Don't process data on a non-GET request. // Don't process data on a non-GET request.
if (params.type !== 'GET' && !Backbone.emulateJSON) { if (params.type !== 'GET' && !options.emulateJSON) {
params.processData = false; params.processData = false;
} }

View File

@@ -8,6 +8,10 @@
sync: Backbone.sync, sync: Backbone.sync,
emulateHTTP: Backbone.emulateHTTP,
emulateJSON: Backbone.emulateJSON,
setup: function() { setup: function() {
var env = this; var env = this;
@@ -32,6 +36,8 @@
this.ajaxSettings = null; this.ajaxSettings = null;
Backbone.sync = this.sync; Backbone.sync = this.sync;
Backbone.ajax = this.ajax; Backbone.ajax = this.ajax;
Backbone.emulateHTTP = this.emulateHTTP;
Backbone.emulateJSON = this.emulateJSON;
} }
}); });

View File

@@ -889,4 +889,12 @@ $(document).ready(function() {
deepEqual(changes, ['a',1,'item']); deepEqual(changes, ['a',1,'item']);
}); });
test("#1791 - `attributes` is available for `parse`", function() {
var Model = Backbone.Model.extend({
parse: function() { this.has('a'); } // shouldn't throw an error
});
var model = new Model(null, {parse: true});
expect(0);
});
}); });

View File

@@ -492,4 +492,9 @@ $(document).ready(function() {
new Router; new Router;
}); });
test("#1794 - Trailing space in fragments.", 1, function() {
var history = new Backbone.History;
strictEqual(history.getFragment('fragment '), 'fragment');
});
}); });

View File

@@ -64,8 +64,10 @@ $(document).ready(function() {
}); });
test("update with emulateHTTP and emulateJSON", 7, function() { test("update with emulateHTTP and emulateJSON", 7, function() {
Backbone.emulateHTTP = Backbone.emulateJSON = true; library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); emulateHTTP: true,
emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest'); equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST'); equal(this.ajaxSettings.type, 'POST');
equal(this.ajaxSettings.dataType, 'json'); equal(this.ajaxSettings.dataType, 'json');
@@ -74,12 +76,12 @@ $(document).ready(function() {
equal(data.id, '2-the-tempest'); equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare'); equal(data.author, 'Tim Shakespeare');
equal(data.length, 123); equal(data.length, 123);
Backbone.emulateHTTP = Backbone.emulateJSON = false;
}); });
test("update with just emulateHTTP", 6, function() { test("update with just emulateHTTP", 6, function() {
Backbone.emulateHTTP = true; library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); emulateHTTP: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest'); equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST'); equal(this.ajaxSettings.type, 'POST');
equal(this.ajaxSettings.contentType, 'application/json'); equal(this.ajaxSettings.contentType, 'application/json');
@@ -87,12 +89,12 @@ $(document).ready(function() {
equal(data.id, '2-the-tempest'); equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare'); equal(data.author, 'Tim Shakespeare');
equal(data.length, 123); equal(data.length, 123);
Backbone.emulateHTTP = false;
}); });
test("update with just emulateJSON", 6, function() { test("update with just emulateJSON", 6, function() {
Backbone.emulateJSON = true; library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}, {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest'); equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'PUT'); equal(this.ajaxSettings.type, 'PUT');
equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded'); equal(this.ajaxSettings.contentType, 'application/x-www-form-urlencoded');
@@ -100,7 +102,6 @@ $(document).ready(function() {
equal(data.id, '2-the-tempest'); equal(data.id, '2-the-tempest');
equal(data.author, 'Tim Shakespeare'); equal(data.author, 'Tim Shakespeare');
equal(data.length, 123); equal(data.length, 123);
Backbone.emulateJSON = false;
}); });
test("read model", 3, function() { test("read model", 3, function() {
@@ -121,12 +122,13 @@ $(document).ready(function() {
test("destroy with emulateHTTP", 3, function() { test("destroy with emulateHTTP", 3, function() {
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'}); library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
Backbone.emulateHTTP = Backbone.emulateJSON = true; library.first().destroy({
library.first().destroy(); emulateHTTP: true,
emulateJSON: true
});
equal(this.ajaxSettings.url, '/library/2-the-tempest'); equal(this.ajaxSettings.url, '/library/2-the-tempest');
equal(this.ajaxSettings.type, 'POST'); equal(this.ajaxSettings.type, 'POST');
equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}'); equal(JSON.stringify(this.ajaxSettings.data), '{"_method":"DELETE"}');
Backbone.emulateHTTP = Backbone.emulateJSON = false;
}); });
test("urlError", 2, function() { test("urlError", 2, function() {
@@ -162,6 +164,32 @@ $(document).ready(function() {
this.ajaxSettings.error(); this.ajaxSettings.error();
}); });
test('Use Backbone.emulateHTTP as default.', 2, function() {
var model = new Backbone.Model;
model.url = '/test';
Backbone.emulateHTTP = true;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateHTTP, true);
Backbone.emulateHTTP = false;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateHTTP, false);
});
test('Use Backbone.emulateJSON as default.', 2, function() {
var model = new Backbone.Model;
model.url = '/test';
Backbone.emulateJSON = true;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateJSON, true);
Backbone.emulateJSON = false;
model.sync('create', model);
strictEqual(this.ajaxSettings.emulateJSON, false);
});
test("#1756 - Call user provided beforeSend function.", 4, function() { test("#1756 - Call user provided beforeSend function.", 4, function() {
Backbone.emulateHTTP = true; Backbone.emulateHTTP = true;
var model = new Backbone.Model; var model = new Backbone.Model;