[EMAIL PROTECTED] wrote:
> Hi all,
> I am having trouble with my variable assignments always becoming the
> last variable inside my loop.
>
> ...
>
> In this scenario, id and headline are always becomes the last value in
> the loop and that is what is passed in the fc.selectArticle()
> function. I've tried a few different possible solutions I've found on
> the web, but none seemed to work. I decided to try and give the bind()
> function a go around but I can't seem to figure out how to use it in
> my situation.
>
> Any help would be greatly appreciated.
>
You can use each() or curry(). In this situation, you don't need a
certain scope so curry() is more suitable than bind(). See example below.
- Ken Snyder
// using each() makes each iteration call a function in its own scope
ret.each(function(data, i) {
var item = document.createElement('div');
var id = data.id[i]
var headline = data.headline[i];
var html = headline + "<hr />";
// don't forget to use a unique id value
item.id = "item" + i;
item.onclick = function(){fc.selectArticle(id, headline);};
item.innerHTML = html;
div.appendChild(item);
});
// using curry returns a new function with the current variable values
enclosed in that scope
for (i=0; i<rowCount; i++) {
var item = document.createElement('div');
var id = ret.id[i]
var headline = ret.headline[i];
var html = headline + "<hr />";
// don't forget to use a unique id value
item.id = "item" + i;
item.onclick = (function(id,
headline){fc.selectArticle(id,headline);}).curry(id, headline);
item.innerHTML = html;
div.appendChild(item);
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---