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

Commit 71db9197 by Jumpei Ogawa

Clearify how to use Getters & Setters

1 parent 478363ca
Showing with 18 additions and 10 deletions
...@@ -134,22 +134,30 @@ Getters and Setters can be defined in 2 ways (you can mix and match these 2 ...@@ -134,22 +134,30 @@ Getters and Setters can be defined in 2 ways (you can mix and match these 2
### Defining as part of a property ### Defining as part of a property
```js ```js
var Foo = sequelize.define('Foo', { var Employee = sequelize.define('Employee', {
name: Sequelize.STRING, name: {
title: {
type : Sequelize.STRING, type : Sequelize.STRING,
allowNull: false, allowNull: false,
get : function() { get : function() {
/* var title = this.getDataValue('title'); // 'this' allows you to access attributes of the instance
do your magic here and return something! return this.getDataValue('name') + ' (' + title + ')';
'this' allows you to access attributes of the model.
example: this.getDataValue('name') works
*/
}, },
set : function(v) { /* do your magic with the input here! */ } },
title: {
type : Sequelize.STRING,
allowNull: false,
set : function(val) {
this.setDataValue('title', val.toUpperCase());
}
} }
}); });
Employee
.create({ name: 'John Doe', title: 'senior engineer' })
.then(function(employee) {
console.log(employee.get('name')); // John Doe (SENIOR ENGINEER)
console.log(employee.get('title')); // SENIOR ENGINEER
})
``` ```
### Defining as part of the model options ### Defining as part of the model options
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!