Search code examples
backbone.jsbackbone-views

In backbone.js view, how do i call another function from jquery $.each?


In my backbone.js view i have a function that has the code below. I would usually call this function using this.addLayerToList(), but since it's in the $.each this. is not what I want. Can anyone help here? How would I call my function addLayerToList from the $.each?

initLayerList: function(){
   $.each(baseLayers, function() {
       this.addLayerToList(this);
   });
},

addLayerToList : function() {
     //...some code here
}

Solution

  • This should work.

    initLayerList: function(){
       var that = this;
       $.each(baseLayers, function(idx, layer) {
           that.addLayerToList(layer);
       });
    },
    
    addLayerToList : function() {
         //...some code here
    }