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

You need to sign in or sign up before continuing.
Commit 0e11c0ce by Vangelis Tsoumenis

Changed Model.prototype.save to optionally accept an array of fieldnames to upda…

…te. In that case other fields will not be updated.
1 parent cc03a046
...@@ -36,8 +36,8 @@ BelongsTo.prototype.injectSetter = function(obj) { ...@@ -36,8 +36,8 @@ BelongsTo.prototype.injectSetter = function(obj) {
obj[accessor] = function(associatedObject) { obj[accessor] = function(associatedObject) {
obj[self.identifier] = associatedObject ? associatedObject.id : null obj[self.identifier] = associatedObject ? associatedObject.id : null
return obj.save() return obj.save([self.identifier]) // passes the changed field to save, so only that field get updated.
} }
return this return this
} }
\ No newline at end of file
...@@ -51,7 +51,13 @@ Model.prototype.query = function() { ...@@ -51,7 +51,13 @@ Model.prototype.query = function() {
return s.query.apply(s, args) return s.query.apply(s, args)
} }
Model.prototype.save = function() { Model.prototype.save = function(fields) {
// if an array with field names is passed to save() only those fields will be updated.
var values = fields ? {} : this.values;
if(fields){
Utils._.each(fields, function(field) { this.values[field] && (values[field] = this.values[field]) }, this);
}
var attr = this.options.underscored ? 'updated_at' : 'updatedAt' var attr = this.options.underscored ? 'updated_at' : 'updatedAt'
if(this.hasOwnProperty(attr)) if(this.hasOwnProperty(attr))
...@@ -70,7 +76,7 @@ Model.prototype.save = function() { ...@@ -70,7 +76,7 @@ Model.prototype.save = function() {
return eventEmitter.run() return eventEmitter.run()
} else { } else {
var identifier = this.options.hasPrimaryKeys ? this.definition.primaryKeys : this.id var identifier = this.options.hasPrimaryKeys ? this.definition.primaryKeys : this.id
return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier)) return this.query(QueryGenerator.updateQuery(this.definition.tableName, values, identifier))
} }
} }
...@@ -169,4 +175,4 @@ Model.prototype.equalsOneOf = function(others) { ...@@ -169,4 +175,4 @@ Model.prototype.equalsOneOf = function(others) {
} }
/* Add the instance methods to Model */ /* Add the instance methods to Model */
Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct}) Utils._.map(Mixin.instanceMethods, function(fct, name) { Model.prototype[name] = fct})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!