Merge pull request #10 from strongloop/internal-prop-prefix
Fix the conflicts between MongoDB _id & juggler's internal prefix
This commit is contained in:
commit
7ddc84747f
|
@ -230,7 +230,7 @@ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data
|
||||||
this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
|
this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) {
|
||||||
var obj;
|
var obj;
|
||||||
if (data) {
|
if (data) {
|
||||||
inst._initProperties(data, true);
|
inst._initProperties(data, false);
|
||||||
obj = inst;
|
obj = inst;
|
||||||
} else {
|
} else {
|
||||||
obj = null;
|
obj = null;
|
||||||
|
@ -327,7 +327,7 @@ DataAccessObject.findById = function find(id, cb) {
|
||||||
setIdValue(this, data, id);
|
setIdValue(this, data, id);
|
||||||
}
|
}
|
||||||
obj = new this();
|
obj = new this();
|
||||||
obj._initProperties(data, true);
|
obj._initProperties(data, false);
|
||||||
}
|
}
|
||||||
cb(err, obj);
|
cb(err, obj);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
@ -426,7 +426,7 @@ DataAccessObject.find = function find(params, cb) {
|
||||||
data.forEach(function (d, i) {
|
data.forEach(function (d, i) {
|
||||||
var obj = new constr();
|
var obj = new constr();
|
||||||
|
|
||||||
obj._initProperties(d, true, params.fields);
|
obj._initProperties(d, false, params.fields);
|
||||||
|
|
||||||
if (params && params.include && params.collect) {
|
if (params && params.include && params.collect) {
|
||||||
data[i] = obj.__cachedRelations[params.collect];
|
data[i] = obj.__cachedRelations[params.collect];
|
||||||
|
@ -620,7 +620,7 @@ DataAccessObject.prototype.save = function (options, callback) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err, inst);
|
return callback(err, inst);
|
||||||
}
|
}
|
||||||
inst._initProperties(data, true);
|
inst._initProperties(data, false);
|
||||||
updateDone.call(inst, function () {
|
updateDone.call(inst, function () {
|
||||||
saveDone.call(inst, function () {
|
saveDone.call(inst, function () {
|
||||||
callback(err, inst);
|
callback(err, inst);
|
||||||
|
|
|
@ -267,7 +267,9 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
||||||
return this.__dataWas && this.__dataWas[attr];
|
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 () {
|
get: function () {
|
||||||
return this.__data && this.__data[attr];
|
return this.__data && this.__data[attr];
|
||||||
},
|
},
|
||||||
|
|
11
lib/model.js
11
lib/model.js
|
@ -100,7 +100,16 @@ ModelBaseClass.prototype._initProperties = function (data, applySetters) {
|
||||||
|
|
||||||
if (applySetters === true) {
|
if (applySetters === true) {
|
||||||
Object.keys(data).forEach(function (attr) {
|
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];
|
self[attr] = self.__data[attr] || data[attr];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue