Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Return saved object from Factory.create method #4

Merged
merged 1 commit into from

2 participants

@szimek

Currently Factory.create returns the result of Factory.build method, not the result of model.save method.

This pull request fixes this issue.

@cadwallion
Collaborator

:facepalm: I can't believe I missed that. Perhaps most of my uses with it don't need much of a difference? Either way, merging this in. Thanks for the contribution!

@cadwallion cadwallion merged commit 40c4718 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 23, 2012
  1. @szimek
This page is out of date. Refresh to see the latest.
Showing with 20 additions and 7 deletions.
  1. +3 −3 index.js
  2. +14 −3 spec/factory_spec.js
  3. +3 −1 spec/spec_helper.js
View
6 index.js
@@ -73,11 +73,11 @@ module.exports = {
data = {};
}
- this.build(model, data, function(err, obj){
+ this.build(model, data, function(err, builtObj){
if (err)
throw(err);
- obj.save(function(error) {
- callback(error, obj);
+ builtObj.save(function(error, createdObj) {
+ callback(error, createdObj);
})
});
}
View
17 spec/factory_spec.js
@@ -38,14 +38,14 @@ describe('Factory#build', function() {
})
it('uses the stored definition to seed attribute values', function() {
- var object = Factory.build('test')
+ var object = Factory.build('test')
expect(object.name).toEqual('Test Model')
expect(object.real).toEqual(false)
expect(object.dynamic).toEqual("dynamic")
})
it('takes an optional parameter to override stores attributes', function() {
- var object = Factory.build('test', { real: true })
+ var object = Factory.build('test', { real: true })
expect(object.name).toEqual('Test Model')
expect(object.real).toEqual(true)
})
@@ -95,7 +95,7 @@ describe('Factory#create', function() {
it('should override attributes like Factory#build', function() {
var object;
Factory.create('test', { name: function(){return 'Test Object';}, dynamic: "another value", called: function(cbk){return cbk(null, 'new value');} }, function(e, o) {
- object = o;
+ object = o;
})
waitsFor(function() { return object != null}, 'object creation failed', 1000);
runs(function() {
@@ -104,4 +104,15 @@ describe('Factory#create', function() {
expect(object.called).toEqual('new value');
})
})
+
+ it('should returned saved object', function() {
+ var object;
+ Factory.create('test', function(e, o) {
+ object = o;
+ })
+ waitsFor(function() { return object != null}, 'object creation failed', 1000);
+ runs(function() {
+ expect(object.isNew).toBe(false);
+ })
+ })
})
View
4 spec/spec_helper.js
@@ -1,10 +1,12 @@
Factory = require('../')
TestModel = function TestModel(attributes) {
+ this.isNew = true;
this.name = attributes.name;
this.real = attributes.real;
this.dynamic = attributes.dynamic;
this.called = attributes.called;
}
TestModel.prototype.save = function(callback) {
- callback(null)
+ this.isNew = false;
+ callback(null, this);
}
Something went wrong with that request. Please try again.