سؤال

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?

هل كانت مفيدة؟

المحلول

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));
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top