Skip to content

Ability to add getter and setter methods to Model object#70

Closed
allanca wants to merge 1 commit into
sequelize:masterfrom
studybreak:getters-and-setters
Closed

Ability to add getter and setter methods to Model object#70
allanca wants to merge 1 commit into
sequelize:masterfrom
studybreak:getters-and-setters

Conversation

@allanca

@allanca allanca commented Jul 1, 2011

Copy link
Copy Markdown
Contributor

Simple ability to add getter and setters for properties to Models. Uses the same syntax as classMethods and instanceMethods.

@sdepold

sdepold commented Jul 5, 2011

Copy link
Copy Markdown
Member

can you please provide an example how to use it?

@jalex79

jalex79 commented Jul 21, 2011

Copy link
Copy Markdown

I prefer like in the examples. set a object like this

var myE = entity.build({
id: varId,
name: varName
})

or

var myE = entity.build({});
myE.id: varId;
myE.name: varName;

But if you have to put some logic, u can create the Set method

@sdepold

sdepold commented Jun 20, 2012

Copy link
Copy Markdown
Member

i finally understood what this ticket is about :D i do like

@sdepold

sdepold commented Jun 20, 2012

Copy link
Copy Markdown
Member

a test would be great

@pdasika82

Copy link
Copy Markdown

Created an example for this feature: https://gist.github.com/2964211

@avimar

avimar commented Jul 22, 2012

Copy link
Copy Markdown

I tried this code but I couldn't get it to work. Is this all the code that's needed..?

@dankohn

dankohn commented Dec 28, 2012

Copy link
Copy Markdown
Contributor

Any chance of getting this pulled? Since Sequelize recommends using INTEGER for holding money as cents, I wrote this model, thinking that this patch had already been accepted.

module.exports = function(sequelize, DataTypes){
     return sequelize.define('Variant', {
        color: {
            type: DataTypes.STRING },
        size: {
            type: DataTypes.STRING },
        listPriceCents: {
            type: DataTypes.INTEGER,
            allowNull: false },
        salePriceCents: {
            type: DataTypes.INTEGER,
            allowNull: false },
        childSku: {
            type: DataTypes.STRING,
            unique: true,
            allowNull: false },
        },
 {       getterMethods: {
            listPrice:  function() {
                return this.listPriceCents / 100
                },
            salePrice:  function() {
                return this.salePriceCents / 100
                }}
        ,
        setterMethods: {
            listPrice: function(list) {
                this.listPriceCents = list * 100
                },
            salePrice: function(sale) {
                this.salePriceCents = sale * 100
                }}
        // ProductId
}    )}

@dankohn

dankohn commented Dec 31, 2012

Copy link
Copy Markdown
Contributor

FYI, here's some context on why using setter and getter methods on top of Integer is a better idea than using floats for currency: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

@polanm

polanm commented Dec 31, 2012

Copy link
Copy Markdown

+1 also waiting for this.

@polanm

polanm commented Jan 2, 2013

Copy link
Copy Markdown

Realized the commit was obsolete, ended up writing a fresh one + a test. See pull request: https://github.com/sdepold/sequelize/pull/393

@sdepold

sdepold commented Apr 24, 2013

Copy link
Copy Markdown
Member

as this is quite old. I will check the PR #538

@sdepold sdepold closed this Apr 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants