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

Commit 18101670 by Felix Becker Committed by Jan Aagaard Meier

Move logic from Model.build to constructor (#6093)

Makes Model.build() and new Model() identical except that build() can also take an array
Makes values parameter optional
1 parent f6cb74ae
Showing with 31 additions and 21 deletions
...@@ -1701,37 +1701,20 @@ class Model { ...@@ -1701,37 +1701,20 @@ class Model {
} }
/** /**
* Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty. * Builds a new model instance.
*
* @param {Object} values * @param {Object} [(values|values[])={}] An object of key value pairs or an array of such. If an array, the function will return an array of instances.
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.raw=false] If set to true, values will ignore field and virtual setters. * @param {Boolean} [options.raw=false] If set to true, values will ignore field and virtual setters.
* @param {Boolean} [options.isNewRecord=true] * @param {Boolean} [options.isNewRecord=true]
* @param {Array} [options.include] an array of include options - Used to build prefetched/included model instances. See `set` * @param {Array} [options.include] an array of include options - Used to build prefetched/included model instances. See `set`
* *
* @return {Instance} * @return {(Model|Model[])}
*/ */
static build(values, options) { // testhint options:none static build(values, options) { // testhint options:none
if (Array.isArray(values)) { if (Array.isArray(values)) {
return this.bulkBuild(values, options); return this.bulkBuild(values, options);
} }
options = _.extend({
isNewRecord: true,
$schema: this.$schema,
$schemaDelimiter: this.$schemaDelimiter
}, options || {});
if (options.attributes) {
options.attributes = options.attributes.map(attribute => Array.isArray(attribute) ? attribute[1] : attribute);
}
if (!options.includeValidated) {
this._conformOptions(options, this);
if (options.include) {
this._expandIncludeAll(options);
this._validateIncludedElements(options);
}
}
return new this(values, options); return new this(values, options);
} }
...@@ -2623,7 +2606,34 @@ class Model { ...@@ -2623,7 +2606,34 @@ class Model {
return this.name; return this.name;
} }
/**
* Builds a new model instance.
* @param {Object} [values={}] an object of key value pairs
* @param {Object} [options]
* @param {Boolean} [options.raw=false] If set to true, values will ignore field and virtual setters.
* @param {Boolean} [options.isNewRecord=true]
* @param {Array} [options.include] an array of include options - Used to build prefetched/included model instances. See `set`
*/
constructor(values, options) { constructor(values, options) {
values = values || {};
options = _.extend({
isNewRecord: true,
$schema: this.constructor.$schema,
$schemaDelimiter: this.constructor.$schemaDelimiter
}, options || {});
if (options.attributes) {
options.attributes = options.attributes.map(attribute => Array.isArray(attribute) ? attribute[1] : attribute);
}
if (!options.includeValidated) {
this.constructor._conformOptions(options, this.constructor);
if (options.include) {
this.constructor._expandIncludeAll(options);
this.constructor._validateIncludedElements(options);
}
}
this.dataValues = {}; this.dataValues = {};
this._previousDataValues = {}; this._previousDataValues = {};
this._changed = {}; this._changed = {};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!