Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13402

Backbone set method, pass a variable as the key?

Im trying to pass an argument which would dynamically set the key in the backbone set method.

I pass it as a string into the constructor of my function like this

this.keyStat('points', 1)

That passes points, but when I set the model it creates the attribute stat not recognizing it as a variable.

keyStat: function(stat, number) {
    var addStat = parseInt(this.model.get(stat)) + number;       
    console.log(this.model.set({stat: addStat}));
}

I am trying to build this function so that I dont have to repeat a lot of code and bloat my project, but I am not sure how I can pass that into the set method so that it recognizes my argument.

My question is how do I pass an argument/variable to the backbone set methods key?

Upvotes: 0

Views: 78

Answers (1)

michal.samluk
michal.samluk

Reputation: 352

set method can take a key as a first argument and value as a second (unless first argument is an object)

keyStat: function(stat, number) {
  var addStat = parseInt(this.model.get(stat)) + number;       
  console.log(this.model.set(stat, addStat));
}

Upvotes: 1

Related Questions