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

Commit af1e4dc9 by Sascha Depold

added class and instance method support

1 parent cb9c088c
// way 1 // way 1
task.title = 'a very different title now' task.title = 'a very different title now'
task.save(function(){}) task.save().on('success', function() {})
// way 2 // way 2
task.updateAttributes({ task.updateAttributes({
title: 'a very different title now' title: 'a very different title now'
}, function(){}) }).on('success', function() {})
...@@ -13,4 +13,4 @@ To get it stored in the database, use the save method and catch the events, ..., ...@@ -13,4 +13,4 @@ To get it stored in the database, use the save method and catch the events, ...,
Now lets change some values and save changes to the database... There are two ways to do that: Now lets change some values and save changes to the database... There are two ways to do that:
<pre><%- koala(".js", partial("code/instances/models-3.ejs")) %></pre> <pre><%- koala(".js", partial("code/instances/update.ejs")) %></pre>
...@@ -16,9 +16,15 @@ var ModelDefinition = module.exports = function(name, attributes, options) { ...@@ -16,9 +16,15 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this.associations = {} this.associations = {}
this.addDefaultAttributes() this.addDefaultAttributes()
this.addOptionalClassMethods()
} }
Utils.addEventEmitter(ModelDefinition) Utils.addEventEmitter(ModelDefinition)
ModelDefinition.prototype.addOptionalClassMethods = function() {
var self = this
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct })
}
ModelDefinition.prototype.addDefaultAttributes = function() { ModelDefinition.prototype.addDefaultAttributes = function() {
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}} var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
, self = this , self = this
...@@ -117,6 +123,7 @@ ModelDefinition.prototype.build = function(values, options) { ...@@ -117,6 +123,7 @@ ModelDefinition.prototype.build = function(values, options) {
instance.addAttribute(name, value) instance.addAttribute(name, value)
} }
}) })
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct })
Utils._.each(this.associations, function(association, associationName) { Utils._.each(this.associations, function(association, associationName) {
association.injectGetter(instance) association.injectGetter(instance)
......
...@@ -62,5 +62,17 @@ module.exports = { ...@@ -62,5 +62,17 @@ module.exports = {
'tablenames should be pluralized if they are not frozen': function() { 'tablenames should be pluralized if they are not frozen': function() {
var User = sequelize.define('User', {}, {freezeTableName: false}) var User = sequelize.define('User', {}, {freezeTableName: false})
assert.eql(User.tableName, 'Users') assert.eql(User.tableName, 'Users')
},
'it should add the passed class/instance methods': function() {
var User = sequelize.define('User', {}, {
classMethods: { doSmth: function(){ return 1 } },
instanceMethods: { makeItSo: function(){ return 2}}
})
assert.isDefined(User.doSmth)
assert.eql(User.doSmth(), 1)
assert.isUndefined(User.makeItSo)
assert.isDefined(User.build().makeItSo)
assert.eql(User.build().makeItSo(), 2)
} }
} }
\ 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!