Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

onLoad method now uses a callback

  • Loading branch information...
commit b50fb9df72d2a299759cbb9d226101b03c8f9965 1 parent 95912f1
Will Brady authored
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
5 README.md
@@ -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
33 lib/connection.js
@@ -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
3  lib/sqltree.js
@@ -162,9 +162,6 @@ var SqlTree = Class.extend({
}
result[column.propertyName] = val;
}
- if (result._getModel().onLoad) {
- result._getModel().onLoad(result);
- }
return result;
}
});
View
3  test/select.js
@@ -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", {
Please sign in to comment.
Something went wrong with that request. Please try again.