mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Added Backbone.emulateJSON to enable the current behavior of syncing and made sending the body as application/json without a wrapping model param, the default
This commit is contained in:
60
test/sync.js
60
test/sync.js
@@ -36,10 +36,23 @@ $(document).ready(function() {
|
||||
equals(lastRequest.url, '/library');
|
||||
equals(lastRequest.type, 'POST');
|
||||
equals(lastRequest.dataType, 'json');
|
||||
var data = JSON.parse(lastRequest.data);
|
||||
equals(data.title, 'The Tempest');
|
||||
equals(data.author, 'Bill Shakespeare');
|
||||
equals(data.length, 123);
|
||||
});
|
||||
|
||||
test("sync: create with emulateJSON", function() {
|
||||
Backbone.emulateJSON = true;
|
||||
library.add(library.create(attrs));
|
||||
equals(lastRequest.url, '/library');
|
||||
equals(lastRequest.type, 'POST');
|
||||
equals(lastRequest.dataType, 'json');
|
||||
var data = JSON.parse(lastRequest.data.model);
|
||||
equals(data.title, 'The Tempest');
|
||||
equals(data.author, 'Bill Shakespeare');
|
||||
equals(data.length, 123);
|
||||
Backbone.emulateJSON = false;
|
||||
});
|
||||
|
||||
test("sync: update", function() {
|
||||
@@ -47,25 +60,53 @@ $(document).ready(function() {
|
||||
equals(lastRequest.url, '/library/1-the-tempest');
|
||||
equals(lastRequest.type, 'PUT');
|
||||
equals(lastRequest.dataType, 'json');
|
||||
var data = JSON.parse(lastRequest.data.model);
|
||||
var data = JSON.parse(lastRequest.data);
|
||||
equals(data.id, '1-the-tempest');
|
||||
equals(data.title, 'The Tempest');
|
||||
equals(data.author, 'William Shakespeare');
|
||||
equals(data.length, 123);
|
||||
});
|
||||
|
||||
test("sync: update with emulateHttp", function() {
|
||||
Backbone.emulateHttp = true;
|
||||
test("sync: update with emulateHTTP", function() {
|
||||
Backbone.emulateHTTP = true;
|
||||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'POST');
|
||||
equals(lastRequest.dataType, 'json');
|
||||
equals(lastRequest.data._method, 'PUT');
|
||||
Backbone.emulateHTTP = false;
|
||||
});
|
||||
|
||||
test("sync: update with emulateJSON", function() {
|
||||
Backbone.emulateJSON = true;
|
||||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'PUT');
|
||||
var data = JSON.parse(lastRequest.data.model);
|
||||
equals(lastRequest.dataType, 'json');
|
||||
equals(data.id, '2-the-tempest');
|
||||
equals(data.title, 'The Tempest');
|
||||
equals(data.author, 'Tim Shakespeare');
|
||||
equals(data.length, 123);
|
||||
Backbone.emulateJSON = false;
|
||||
});
|
||||
|
||||
test("sync: update with emulateHTTP and emulateJSON", function() {
|
||||
Backbone.emulateHTTP = true;
|
||||
Backbone.emulateJSON = true;
|
||||
library.first().save({id: '2-the-tempest', author: 'Tim Shakespeare'});
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'POST');
|
||||
equals(lastRequest.dataType, 'json');
|
||||
equals(lastRequest.data._method, 'PUT');
|
||||
var data = JSON.parse(lastRequest.data.model);
|
||||
equals(lastRequest.dataType, 'json');
|
||||
equals(data.id, '2-the-tempest');
|
||||
equals(data.title, 'The Tempest');
|
||||
equals(data.author, 'Tim Shakespeare');
|
||||
equals(data.length, 123);
|
||||
Backbone.emulateHTTP = false;
|
||||
Backbone.emulateJSON = false;
|
||||
});
|
||||
|
||||
test("sync: read model", function() {
|
||||
@@ -76,19 +117,26 @@ $(document).ready(function() {
|
||||
});
|
||||
|
||||
test("sync: destroy", function() {
|
||||
Backbone.emulateHttp = false;
|
||||
library.first().destroy();
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'DELETE');
|
||||
ok(_.isEmpty(lastRequest.data));
|
||||
});
|
||||
|
||||
test("sync: destroy with emulateHttp", function() {
|
||||
Backbone.emulateHttp = true;
|
||||
test("sync: destroy with emulateJSON", function() {
|
||||
library.first().destroy();
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'DELETE');
|
||||
ok(_.isEmpty(lastRequest.data));
|
||||
});
|
||||
|
||||
test("sync: destroy with emulateHTTP", function() {
|
||||
Backbone.emulateHTTP = true;
|
||||
library.first().destroy();
|
||||
equals(lastRequest.url, '/library/2-the-tempest');
|
||||
equals(lastRequest.type, 'POST');
|
||||
equals(JSON.stringify(lastRequest.data), '{"_method":"DELETE"}');
|
||||
Backbone.emulateHTTP = false;
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user