Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[email protected] compliance #77

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 24 additions & 6 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
});