diff --git a/core.js b/core.js index 95a6467..c14bf4e 100644 --- a/core.js +++ b/core.js @@ -67,7 +67,8 @@ module.exports = function (xhr) { // Ensure that we have the appropriate request data. if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { - params.json = options.attrs || model.toJSON(options); + params.body = options.attrs || model.toJSON(options); + params.json = true; } // If passed a data param, we add it to the URL or body depending on request type @@ -82,7 +83,7 @@ module.exports = function (xhr) { // For older servers, emulate JSON by encoding the request into an HTML-form. if (options.emulateJSON) { params.headers['content-type'] = 'application/x-www-form-urlencoded'; - params.body = params.json ? {model: params.json} : {}; + params.body = params.json ? {model: params.body} : {}; delete params.json; } diff --git a/package.json b/package.json index 0215d6c..57c9d89 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "media-type": "0.3.0", "qs": "^6.1.0", "request": "^2.55.0", - "xhr": "^2.0.5" + "xhr": "^2.3.1" }, "devDependencies": { "ampersand-model": "^7.0.0", diff --git a/test/unit.js b/test/unit.js index f5bcb90..8d2d007 100644 --- a/test/unit.js +++ b/test/unit.js @@ -115,8 +115,8 @@ test('create', function (t) { })); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); + var data = reqStub.recentOpts.body; t.equal(data.title, 'The Tempest'); t.equal(data.author, 'Bill Shakespeare'); t.equal(data.length, 123); @@ -130,8 +130,8 @@ test('update', function (t) { })); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'PUT'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); + var data = reqStub.recentOpts.body; t.equal(data.id, '1-the-tempest'); t.equal(data.author, 'William Shakespeare'); t.end(); @@ -163,8 +163,8 @@ test('update with just emulateHTTP', function (t) { }); t.equal(reqStub.recentOpts.url, '/library'); t.equal(reqStub.recentOpts.method, 'POST'); - t.ok(reqStub.recentOpts.json, 'body passed as json'); - var data = reqStub.recentOpts.json; + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); + var data = reqStub.recentOpts.body; t.equal(data.id, '2-the-tempest'); t.equal(data.author, 'Tim Shakespeare'); t.equal(data.length, 123); @@ -352,3 +352,21 @@ test('should parse json for different media types', function (t) { }); }); +test('passing `body` in the opts should take precedence over the model\'s data', function (t) { + var model = modelStub({ + title: 'The Tempest', + author: 'Bill Shakespeare', + length: 123 + }); + + sync('create', model, { + body: { + rating: "5" + } + }); + + t.equal(reqStub.recentOpts.json, true, 'json is set to true'); + var data = reqStub.recentOpts.body; + t.equal(data.rating, '5'); + t.end(); +}); \ No newline at end of file