Merge pull request #10 from strongloop/internal-prop-prefix

Fix the conflicts between MongoDB _id & juggler's internal prefix
This commit is contained in:
Raymond Feng 2013-08-30 17:44:21 -07:00
commit 7ddc84747f
3 changed files with 17 additions and 6 deletions

View File

@ -230,7 +230,7 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
var obj;
if (data) {
inst._initProperties(data, true);
inst._initProperties(data, false);
obj = inst;
} else {
obj = null;
@ -327,7 +327,7 @@ DataAccessObject.findById = function find(id, cb) {
setIdValue(this, data, id);
}
obj = new this();
obj._initProperties(data, true);
obj._initProperties(data, false);
}
cb(err, obj);
}.bind(this));
@ -426,7 +426,7 @@ DataAccessObject.find = function find(params, cb) {
data.forEach(function (d, i) {
var obj = new constr();
obj._initProperties(d, true, params.fields);
obj._initProperties(d, false, params.fields);
if (params && params.include && params.collect) {
data[i] = obj.__cachedRelations[params.collect];
@ -620,7 +620,7 @@ DataAccessObject.prototype.save = function (options, callback) {
if (err) {
return callback(err, inst);
}
inst._initProperties(data, true);
inst._initProperties(data, false);
updateDone.call(inst, function () {
saveDone.call(inst, function () {
callback(err, inst);

View File

@ -267,7 +267,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
return this.__dataWas && this.__dataWas[attr];
});
Object.defineProperty(ModelClass.prototype, '_' + attr, {
// FIXME: [rfeng] Do we need to keep the raw data?
// Use $ as the prefix to avoid conflicts with properties such as _id
Object.defineProperty(ModelClass.prototype, '$' + attr, {
get: function () {
return this.__data && this.__data[attr];
},

View File

@ -100,7 +100,16 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) {
if (applySetters === true) {
Object.keys(data).forEach(function (attr) {
if((attr in properties) || (attr in ctor.relations) || strict === false) {
if((attr in properties) || (attr in ctor.relations)) {
self[attr] = self.__data[attr] || data[attr];
}
});
}
// Set the unknown properties as properties to the object
if(strict === false) {
Object.keys(data).forEach(function (attr) {
if(!(attr in properties)) {
self[attr] = self.__data[attr] || data[attr];
}
});