不要怂,就是干,撸起袖子干!

Commit 5c59702a by Mick Hansen

feat(instance): provide plain: true option to get() that will reduce included in…

…stances to plain value objects
1 parent bea65490
......@@ -33,7 +33,7 @@ module.exports = (function() {
this.dataValues = {};
this._previousDataValues = {};
this.__options = this.Model.options;
this.options = options;
this.options = options || {};
this.hasPrimaryKeys = this.Model.options.hasPrimaryKeys;
this.__eagerlyLoadedAssociations = [];
/**
......@@ -165,29 +165,45 @@ module.exports = (function() {
* If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key.
*
* @param {String} [key]
* @param {Object} [options]
* @param {Boolean} [options.plain=false] If set to true, included instances will be returned as plain objects
* @return {Object|any}
*/
Instance.prototype.get = function(key) {
Instance.prototype.get = function(key, options) {
if (options === undefined && typeof key === "object") {
options = key;
key = undefined;
}
if (key) {
if (this._customGetters[key]) {
return this._customGetters[key].call(this, key);
}
if (options && options.plain && this.options.include && this.options.includeNames.indexOf(key) !== -1) {
return this.dataValues[key].get({plain: options.plain});
}
return this.dataValues[key];
}
if (this._hasCustomGetters) {
if (this._hasCustomGetters || (options && options.plain && this.options.include)) {
var values = {}
, _key;
for (_key in this._customGetters) {
if (this._customGetters.hasOwnProperty(_key)) {
values[_key] = this.get(_key);
if (this._hasCustomGetters) {
for (_key in this._customGetters) {
if (this._customGetters.hasOwnProperty(_key)) {
values[_key] = this.get(_key);
}
}
}
for (_key in this.dataValues) {
if (!values.hasOwnProperty(_key) && this.dataValues.hasOwnProperty(_key)) {
values[_key] = this.dataValues[_key];
if (options.plain && this.options.include && this.options.includeNames.indexOf(_key) !== -1) {
values[_key] = this.dataValues[_key].get({plain: options.plain});
} else {
values[_key] = this.dataValues[_key];
}
}
}
return values;
......@@ -282,7 +298,7 @@ module.exports = (function() {
} else {
// Check if we have included models, and if this key matches the include model names/aliases
if (this.options && this.options.include && this.options.includeNames.indexOf(key) !== -1 && value) {
if (this.options.include && this.options.includeNames.indexOf(key) !== -1 && value) {
// Pass it on to the include handler
this._setInclude(key, value, options);
return;
......@@ -883,7 +899,9 @@ module.exports = (function() {
* @return {object}
*/
Instance.prototype.toJSON = function() {
return this.get();
return this.get({
plain: true
});
};
// private
......
......@@ -294,6 +294,7 @@ module.exports = (function() {
return self._virtualAttributes.indexOf(key) !== -1;
});
this.Instance.prototype.$Model =
this.Instance.prototype.Model = this;
this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!