title: Backbone and jQuery slider
body: <p>So I've been struggling for quire sometime to do this. I am building a dynamic form generator tool. One of the functions is that the user should be able to use a jQuery slider to select the font size. I use Underscore templates to create divs for the slider. So everytime a user selects a Label input, this underscore template is loaded and I call the slider() fn to initialize it. Here's is the part of the code that does this</p>
  
  <p>UnderscoreTemplate</p>
  
  <pre><code>&lt;!-- template for plain text inpt to be shown in form generate --&gt;
      &lt;script type="text/template" id="text-generate-template"&gt;       
          &lt;div class="row"&gt;       
              &lt;div class="col-sm-10"&gt;
                  &lt;input type="text"  placeholder="Enter text here" class="label-name form-control" value="&lt;%=model.get('label')%&gt;"&gt;
              &lt;/div&gt;              
              &lt;button type="button" class="close" id="remove-element" &gt;&amp;times;&lt;/button&gt;
          &lt;/div&gt;          
          &lt;div class="row col-sm-6"  style="margin-top:10px"&gt;
              &lt;div id="slider-&lt;%=model.cid%&gt;"&gt;&lt;/div&gt;
              &lt;small&gt;Font Size:  &lt;%=model.get('fontSize')%&gt;px &lt;/small&gt;            
          &lt;/div&gt;  
      &lt;/script&gt;
  </code></pre>
  
  <p>So as per the code ablove, each dynamically loaded element has a unique slider with a unique ID.</p>
  
  <p>The Backbone Model extends from a Base element called "Element"</p>
  
  <pre><code>    var TextElement = Element.extend({
              defaults:function(){
                  return _.extend({}, Element.prototype.defaults,{
                      name: 'PlainText',
                      generateTemplateType: 'text-generate-template',
                      previewTemplateType:'text-template',
                      textAlign: 'center'     
                  });
              }                               
          });
  </code></pre>
  
  <p>The view which is responsible for generating the html is </p>
  
  <pre><code>var ElementView = Backbone.View.extend({
  
              tagName: 'li',
              events : {              
  
                  },
              initialize : function(){
                  this.listenTo(this.model,'change', this.render);        
                  this.render();
              },
              render : function(){                            
                  var htmlContent =  $('#'+this.model.get('generateTemplateType')).html();                        
                  var content = _.template( htmlContent, {elementType : elementTypes, model : this.model} );                                      
                  this.$el.html( content );           
                  me = this;                  
                  if(this.model.get('name') == 'PlainText'){                      
                      this.$el.find('#slider-'+this.model.cid).slider({
                           min: 10,
                           max:40,
                           step: 1,
                           value:me.model.get('fontSize') &gt; 9 ? me.model.get('fontSize') : 24,
                           change: function(event, ui) {
                              me.slide(event, ui.value);
                          }
                      }); 
                  }
  
                  return this;
              },              
  
              slide: function(event, index){                  
                  console.log("the models id in slide function -&gt; "+this.model.cid);
                  this.model.set('fontSize', index);                  
              }
          });
  </code></pre>
  
  <p>So what I do here is that every time I detect a change, I update this model's fontSize to the new value passed in through the Slider.</p>
  
  <p>THe collection to which this model belongs to recognises this changes and rerenders itself.</p>
  
  <p>When there is only one such element on the page, then everything works fine. The moment the user adds more than one element and tried to change the font size for each item, then only the font size of the last element is changed. Regardless of whether I slide the first , second or the third element it always sets the change to the last model in the collection and the last model get rerendered with the incorrect font size value.</p>
  
  <p>I tried console logging the id of the model in the slide function and it always seems to return the last models ID regardless of which slider I use.</p>
  
  <p>What am I doing wrong here??</p>