Skip to content

Commit

Permalink
Now allows for a createDefault function when POST requests are made.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewest committed Jan 17, 2015
1 parent fce9f5f commit 4a630ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
5 changes: 4 additions & 1 deletion dist/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,14 @@ var Y=s();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(G.
Facade.backend.whenPOST(opts.resource.url)
.respond(function(method, url, data, headers) {
data = data || {};
data = JSON.parse(data);
var route = Facade.findRoute(method, url);

// Perform the POST on the db
var response = route.getSpecialResponseOr(function() {
var item = opts.resource.addItem(JSON.parse(data));
var item = _.isFunction(opts.resource.createDefault) && opts.resource.createDefault(data);
item = item || data;
opts.resource.addItem(item);
return [200, JSON.stringify(item), {}, 'OK'];
})

Expand Down
4 changes: 2 additions & 2 deletions dist/facade.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,14 @@
Facade.backend.whenPOST(opts.resource.url)
.respond(function(method, url, data, headers) {
data = data || {};
data = JSON.parse(data);
var route = Facade.findRoute(method, url);

// Perform the POST on the db
var response = route.getSpecialResponseOr(function() {
var item = opts.resource.addItem(JSON.parse(data));
var item = _.isFunction(opts.resource.createDefault) && opts.resource.createDefault(data);
item = item || data;
opts.resource.addItem(item);
return [200, JSON.stringify(item), {}, 'OK'];
})

Expand Down
14 changes: 12 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@
});
});
describe("#initialize", function() {
var patientResource;
beforeEach(function() {
var patientResource = Facade.resource({
patientResource = Facade.resource({
name: "patient",
url: "/api/provider/patients"
});
Expand Down Expand Up @@ -223,6 +224,15 @@
$httpBackend.flush();
$rootScope.postedItem.should.eql({id: 3, name: "My new patient!"});
});
it("should use the create function when performing the POST route if it's there", function() {
patientResource.createDefault = function(postData) {
var defaultPatient = {id: "pat-12345", name: "Joe Smith"};
return _.extend(defaultPatient, postData);
};
$rootScope.post("patients", {name: "My new patient!"});
$httpBackend.flush();
$rootScope.postedItem.should.eql({id: "pat-12345", name: "My new patient!"});
});
it("should create a DELETE route for each resource item, and perform the DELETE", function() {
$rootScope.getOne("patients", 1);
$httpBackend.flush();
Expand Down Expand Up @@ -362,7 +372,7 @@
grandChildResource.url.should.eql("/api/provider/patients/charges/payments");
});
});
describe.only("#expect", function() {
describe("#expect", function() {
var patientResource;
beforeEach(function() {
patientResource = Facade.resource({
Expand Down

0 comments on commit 4a630ea

Please sign in to comment.