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

Commit f61ada76 by Mick Hansen

refactor(model): move attribute existance cache checks from init to refreshAttributes, closes #2409

1 parent b75154d2
Showing with 68 additions and 70 deletions
......@@ -221,71 +221,9 @@ module.exports = (function() {
this.refreshAttributes();
findAutoIncrementField.call(this);
this._booleanAttributes = [];
this._dateAttributes = [];
this._hstoreAttributes = [];
this._jsonAttributes = [];
this._virtualAttributes = [];
this._defaultValues = {};
this.Instance.prototype.validators = {};
Utils._.each(this.rawAttributes, function(definition, name) {
var type = definition.type._typeName || definition.type;
if (type === DataTypes.BOOLEAN) {
self._booleanAttributes.push(name);
} else if (type === DataTypes.DATE) {
self._dateAttributes.push(name);
} else if (type === DataTypes.HSTORE) {
self._hstoreAttributes.push(name);
} else if (type === DataTypes.JSON) {
self._jsonAttributes.push(name);
} else if (type === DataTypes.VIRTUAL) {
self._virtualAttributes.push(name);
}
if (definition.hasOwnProperty('defaultValue')) {
self._defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue);
}
if (definition.hasOwnProperty('validate')) {
self.Instance.prototype.validators[name] = definition.validate;
}
});
this._hasBooleanAttributes = !!this._booleanAttributes.length;
this._isBooleanAttribute = Utils._.memoize(function(key) {
return self._booleanAttributes.indexOf(key) !== -1;
});
this._hasDateAttributes = !!this._dateAttributes.length;
this._isDateAttribute = Utils._.memoize(function(key) {
return self._dateAttributes.indexOf(key) !== -1;
});
this._hasHstoreAttributes = !!this._hstoreAttributes.length;
this._isHstoreAttribute = Utils._.memoize(function(key) {
return self._hstoreAttributes.indexOf(key) !== -1;
});
this._hasJsonAttributes = !!this._jsonAttributes.length;
this._isJsonAttribute = Utils._.memoize(function(key) {
return self._jsonAttributes.indexOf(key) !== -1;
});
this._hasVirtualAttributes = !!this._virtualAttributes.length;
this._isVirtualAttribute = Utils._.memoize(function(key) {
return self._virtualAttributes.indexOf(key) !== -1;
});
this.Instance.prototype.$Model =
this.Instance.prototype.Model = this;
this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues);
this.tableAttributes = Utils._.omit(this.rawAttributes, this._virtualAttributes);
return this;
};
......@@ -317,14 +255,6 @@ module.exports = (function() {
});
Utils._.each(self.rawAttributes, function(options, attribute) {
options.Model = self;
options.fieldName = attribute;
options._modelAttribute = true;
if (options.field === undefined) {
options.field = attribute;
}
if (options.hasOwnProperty(type)) {
_custom[attribute] = options[type];
}
......@@ -351,6 +281,74 @@ module.exports = (function() {
});
});
this._booleanAttributes = [];
this._dateAttributes = [];
this._hstoreAttributes = [];
this._jsonAttributes = [];
this._virtualAttributes = [];
this._defaultValues = {};
this.Instance.prototype.validators = {};
Utils._.each(this.rawAttributes, function(definition, name) {
var type = definition.type._typeName || definition.type;
definition.Model = self;
definition.fieldName = name;
definition._modelAttribute = true;
if (definition.field === undefined) {
definition.field = name;
}
if (type === DataTypes.BOOLEAN) {
self._booleanAttributes.push(name);
} else if (type === DataTypes.DATE) {
self._dateAttributes.push(name);
} else if (type === DataTypes.HSTORE) {
self._hstoreAttributes.push(name);
} else if (type === DataTypes.JSON) {
self._jsonAttributes.push(name);
} else if (type === DataTypes.VIRTUAL) {
self._virtualAttributes.push(name);
}
if (definition.hasOwnProperty('defaultValue')) {
self._defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue);
}
if (definition.hasOwnProperty('validate')) {
self.Instance.prototype.validators[name] = definition.validate;
}
});
this._hasBooleanAttributes = !!this._booleanAttributes.length;
this._isBooleanAttribute = Utils._.memoize(function(key) {
return self._booleanAttributes.indexOf(key) !== -1;
});
this._hasDateAttributes = !!this._dateAttributes.length;
this._isDateAttribute = Utils._.memoize(function(key) {
return self._dateAttributes.indexOf(key) !== -1;
});
this._hasHstoreAttributes = !!this._hstoreAttributes.length;
this._isHstoreAttribute = Utils._.memoize(function(key) {
return self._hstoreAttributes.indexOf(key) !== -1;
});
this._hasJsonAttributes = !!this._jsonAttributes.length;
this._isJsonAttribute = Utils._.memoize(function(key) {
return self._jsonAttributes.indexOf(key) !== -1;
});
this._hasVirtualAttributes = !!this._virtualAttributes.length;
this._isVirtualAttribute = Utils._.memoize(function(key) {
return self._virtualAttributes.indexOf(key) !== -1;
});
this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues);
this.attributes = this.rawAttributes;
this.tableAttributes = Utils._.omit(this.rawAttributes, this._virtualAttributes);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!