Reputation: 15639
I'm trying to pass an additional object named modelObj
to underscore's _.each
function where as I'm not getting the context inside the loop.
My code goes like this,
showHideBudget: function(contentObj, modelObj){
_.each(contentObj.find('.budgetSec'), function(item){
// ===> Unable to reference modelObj here <===
budgetFlag = true;
return;
}, modelObj)
})
Can someone help?
Edit - Here is what I've made it to work.
Finally found what went wrong, It seems I need to pass both this
and modelObj
to make it work, (like this).
showHideBudget: function(contentObj, modelObj){
_.each(contentObj.find('.budgetSec'), function(item){
// ===> Unable to reference modelObj here <===
budgetFlag = true;
return;
}, modelObj, this)
})
Upvotes: 0
Views: 84
Reputation: 502
From my understanding, that third argument is what this
is bound to within your iteratee function. You should be able to get the reference to modelObj through this
that function that's called for each element of the result of contentObj.find('budgetSec')
.
Reference: What is context in _.each(list, iterator, [context])?
Upvotes: 0
Reputation: 10922
Try it this way instead. You should pass the object to the _.each
showHideBudget: function(contentObj, modelObj){
_.each(contentObj.find('.budgetSec'), function(item){
budgetFlag = true;
return;
}, modelObj);
}
Upvotes: 1