title: How to set a dynamic property on a model with Backbone.js
body: <p>Story:
  I have a lot of properties that needs to be set. Since they have similarities I choose to read out the class the inputbox is part of as property name.</p>
  
  <p>Problem:
  A dynamic property of an object can be set, just like an associative array. So I could do </p>
  
  <pre><code>var customattribute = this.$el.parents('div').attr('id');
  var value = $(e.currentTarget()).val();
  
  this.model.attributes[customattribute] = value;
  </code></pre>
  
  <p>But this wouldn't trigger a change event of the model. I could manually trigger a change event, but this wouldn't update this.model.changedAttributes(), I need to set only the changed attribute, not every attribute.</p>
  
  <p>This of course doesn't work either:</p>
  
  <pre><code>this.model.set(customattribute: value);
  </code></pre>
  
  <p><em>So how would I handle this problem?</em></p>
  
  <p>I have ALOT (200+) attributes that can be set, I wouldn't like to make separate eventlisteners for every attribute unless thats the only way.</p>
  
  <p>Code:</p>
  
  <pre><code>var Display = Backbone.View.extend({
      className: 'display',
      events: {
          'slide .slider' : 'sliderHandler'
      },
      initialize: function(){
          _.bindAll(this, 'render', 'sliderHandler','update');
          this.model.on('change',this.update, this);
      },
      render: function(){
          this.$el.html(_.template(html, {}));
          this.$('.slider').slider();
  
          return this;
      },
      sliderHandler: function(e){
          var slider = $(e.currentTarget);
          var property = slider.parents('div').attr('id');
          var value = slider.slider('value');
  
          this.model.attributes[property] = value;            
      },
      update: function(){
          console.log(this.model.changedAttributes());
          //get changed attribute + value here
  
      },
  });
  </code></pre>
  
  <p>Edit:</p>
  
  <p>The two answers below solved it. Map the attributes to an object and give that to Backbone. Also I found another solution. Instead of an object, model.set() also accepts an array.</p>
  
  <pre><code>model.set(customattribute, value, customattribute2, value2);
  </code></pre>