Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mshick committed Feb 21, 2014
2 parents f757988 + 2a345d2 commit 6e2da61
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/adapters/memory.js
Expand Up @@ -96,7 +96,7 @@ Memory.prototype.fromDb = function(model, data) {
val = new Date(val.toString().replace(/GMT.*$/, 'GMT'));
break;
case 'Boolean':
val = new Boolean(val);
val = Boolean(val);
break;
}
}
Expand Down
24 changes: 17 additions & 7 deletions lib/model.js
Expand Up @@ -380,7 +380,7 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
/**
* Find one record, same as `all`, limited by 1 and return object, not collection,
* if not found, create using data provided as second argument
*
*
* @param {Object} query - search conditions: {where: {test: 'me'}}.
* @param {Object} data - object to create.
* @param {Function} cb - callback called with (err, instance)
Expand Down Expand Up @@ -585,7 +585,7 @@ AbstractClass.iterate = function map(filter, iterator, callback) {

/**
* Find one record, same as `all`, limited by 1 and return object, not collection
*
*
* @param {Object} params - search conditions: {where: {test: 'me'}}
* @param {Function} cb - callback called with (err, instance)
*/
Expand Down Expand Up @@ -673,7 +673,7 @@ AbstractClass.prototype.save = function (options, callback) {

if (!this.id) {
// Pass options and this to create
var data = {
var data = {
data: this,
options: options
};
Expand Down Expand Up @@ -734,11 +734,11 @@ AbstractClass.prototype._adapter = function () {
* Convert instance to Object
*
* @param {Boolean} onlySchema - restrict properties to schema only, default false
* when onlySchema == true, only properties defined in schema returned,
* when onlySchema == true, only properties defined in schema returned,
* otherwise all enumerable properties returned
* @returns {Object} - canonical object representation (no getters and setters)
*/
AbstractClass.prototype.toObject = function (onlySchema) {
AbstractClass.prototype.toObject = function (onlySchema, cachedRelations) {
var data = {};
var ds = this.constructor.schema.definitions[this.constructor.modelName];
var properties = ds.properties;
Expand All @@ -760,6 +760,15 @@ AbstractClass.prototype.toObject = function (onlySchema) {
data[attr] = self[attr];
}
});

if (cachedRelations === true && this.__cachedRelations) {
var relations = this.__cachedRelations;
Object.keys(relations).forEach(function (attr) {
if (!data.hasOwnProperty(attr)) {
data[attr] = relations[attr];
}
});
}
}

return data;
Expand All @@ -770,10 +779,11 @@ AbstractClass.prototype.toObject = function (onlySchema) {
// Object.getOwnPropertyNames(this).indexOf(prop) !== -1;
// };

AbstractClass.prototype.toJSON = function () {
return this.toObject();
AbstractClass.prototype.toJSON = function (cachedRelations) {
return this.toObject(false, cachedRelations);
};


/**
* Delete object from persistence
*
Expand Down

0 comments on commit 6e2da61

Please sign in to comment.