Orm sequelizejs中instanceMethods和getterMethods之间有什么区别?
并且似乎完成了同样的事情,允许访问虚拟密钥。为什么要使用一种方法而不是另一种方法?使用Orm sequelizejs中instanceMethods和getterMethods之间有什么区别?,orm,sequelize.js,Orm,Sequelize.js,并且似乎完成了同样的事情,允许访问虚拟密钥。为什么要使用一种方法而不是另一种方法?使用Model.method()调用实例方法,但是如果有一个名为text的getter方法,则在执行Instance.text时将调用它。类似地,当您执行instance.text='something'时,也会使用setter方法 例如: sequelize.define("aModel", { text: DataTypes.TEXT }, { instanceMethods: {
Model.method()
调用实例方法,但是如果有一个名为text
的getter方法,则在执行Instance.text
时将调用它。类似地,当您执行instance.text='something'
时,也会使用setter方法
例如:
sequelize.define("aModel", {
text: DataTypes.TEXT
}, {
instanceMethods: {
getme1: function() {
return this.text.toUpperCase();
}
},
getterMethods: {
getme2: function() {
return this.text.toUpperCase();
}
}
});
请使用v4中断更改更新此答案:
var Model = sequelize.define("aModel", {
text: DataTypes.TEXT
}, {
instanceMethods: {
getUpperText: function() {
return this.text.toUpperCase();
}
},
getterMethods: {
text: function() {
// use getDataValue to not enter an infinite loop
// http://docs.sequelizejs.com/en/latest/api/instance/#getdatavaluekey-any
return this.getDataValue('text').toUpperCase();
}
},
setterMethods: {
text: function(text) {
// use setDataValue to not enter an infinite loop
// http://docs.sequelizejs.com/en/latest/api/instance/#setdatavaluekey-value
this.setDataValue('text', text.toLowerCase());
}
}
});
Model.create({
text: 'foo'
}).then(function(instance) {
console.log(instance.getDataValue('text')); // foo
console.log(instance.getUpperText()); // FOO
console.log(instance.text); // FOO
instance.text = 'BAR';
console.log(instance.getDataValue('text')) // bar
console.log(instance.text); // BAR
});