Permalink
Browse files

Merge pull request #79 from wbrady/master

Give onLoad a callback
  • Loading branch information...
2 parents 95912f1 + b50fb9d commit ce018cbf7b669efc5b05974ebe901b3e63eb3cea @joeferner committed Jul 10, 2013
Showing with 29 additions and 15 deletions.
  1. +4 −1 README.md
  2. +23 −10 lib/connection.js
  3. +0 −3 lib/sqltree.js
  4. +2 −1 test/select.js
View
@@ -613,11 +613,14 @@ create this method in your model file.
__Arguments__
* obj - The object that was just loaded from the database.
+ * connection - The connection persist is currently using to do the save
+ * callback() - The callback to be called when the onLoad is complete
__Example__
```javascript
-Person.onLoad = function(obj) {
+Person.onLoad = function(obj, connection, callback) {
obj.fullName = obj.firstName + ' ' + obj.lastName;
+ callback();
};
```
<a name="associatedObjectProperties" />
View
@@ -261,6 +261,15 @@ var Connection = Class.extend({
all: function(sqlTree, callback) {
var self = this;
+
+ function callOnLoad(obj, callback) {
+ if (obj._getModel().onLoad) {
+ return obj._getModel().onLoad(obj, self, callback);
+ }
+
+ callback();
+ }
+
var sqlAndValues = this.driver.getSqlFromSqlTree(sqlTree);
if (self.opts.trace) {
console.log(sqlAndValues.sql, sqlAndValues.values);
@@ -272,18 +281,22 @@ var Connection = Class.extend({
}
var objs = sqlTree.toObjects(data);
- // add connections to all the objects
- var i;
- var selfFunc = function() { return self; };
- for (i = 0; i < objs.length; i++) {
- objs[i]._getConnection = selfFunc; // hide from JSON.stringify
- }
+ async.forEach(objs, function(obj, callback) {
+ callOnLoad(obj, callback)
+ }, function() {
+ // add connections to all the objects
+ var i;
+ var selfFunc = function() { return self; };
+ for (i = 0; i < objs.length; i++) {
+ objs[i]._getConnection = selfFunc; // hide from JSON.stringify
+ }
- if (typeof (sqlTree.model) === "function") {
- self._augmentResultsArrayWithHelpers(sqlTree.model(), objs);
- }
+ if (typeof (sqlTree.model) === "function") {
+ self._augmentResultsArrayWithHelpers(sqlTree.model(), objs);
+ }
- callback(null, objs);
+ callback(null, objs);
+ });
});
},
View
@@ -162,9 +162,6 @@ var SqlTree = Class.extend({
}
result[column.propertyName] = val;
}
- if (result._getModel().onLoad) {
- result._getModel().onLoad(result);
- }
return result;
}
});
View
@@ -17,8 +17,9 @@ exports['Select'] = nodeunit.testCase({
"age": type.INTEGER
}).hasMany(this.Phone);
- this.Person.onLoad = function (person) {
+ this.Person.onLoad = function (person, connection, callback) {
person.nameAndAge = person.name + ": " + person.age;
+ callback();
};
this.Company = persist.define("Company", {

0 comments on commit ce018cb

Please sign in to comment.