The problem seems to be that you're trying to use an each()
method, which does not exist on Array.prototype
. I believe you're looking for Array.prototype.forEach()
. Underscore's _.each(array, callback)
method would also work, and is more compatible with older browsers.
This should work:
Template.test.events({
'click .selector': function (e) {
e.preventDefault();
var someArray = [1,2,3,4];
var someVariable = "jquery slector data";
console.log(someVariable);
someArray.forEach(function(val, index) {
console.log(someVariable);
});
}
});
Or, for compatibility:
_.each(someArray, function(val, index) {
console.log(someVariable);
})
I believe your confusion stems from using jQuery's $.each()
method, which works differently from the native forEach
and Underscore's _.each()
. For one, jQuery has a different argument order (notice that I switched the order of the arguments in the code). In general, I'd recommend using Underscore for collection-related functions and use jQuery primarily for DOM manipulation.