2

my rails view looks like this (it is a partial):

=javascript_include_tag :defaults

#scroller
 -@other_images.each do |img|
  .thumbnail_wrapper
    .thumbnail_image
      =image_tag img.image.url(:thumbnail), :class => 'button', :onClick => "replaceImg(#{img.id});",  :onLoad => "setLastID(#{ @other_images[0].id });"

I want to call the function setLastID(#{ @other_images[0].id }); only once, when the whole view loads. However, I couldn't figure out how... this was the only place I could put it where it worked. But now it's been called each time within the loop... Help!!

3 Answers 3

1

This is an example where you want to dynamically generate the javascript in the partial, since ruby variables are required. As long as these sorts of calls are very short, I'm fine with it.

=javascript_include_tag :defaults

#scroller
 -@other_images.each do |img|
  .thumbnail_wrapper
    .thumbnail_image
      =image_tag img.image.url(:thumbnail), :class => 'button', :onClick => "replaceImg(#{img.id});"

:javascript
  setLastID(#{@other_images[0].id});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you fullware, but I tried this and for some reason it isn't working for me. I know because even putting a simple alert in there does nothing. Any idea why??
That view is being loaded by Ajax like this: new Ajax.Updater(this.scroller, url); (by a direct url) Is this somehow stopping direct javascript functions?
1

Have you considered loading a js.erb template instead? You can add to the dom using javascript instead of haml, and you still have access to your Rails variables. If you don't need to use that javascript function setLastID() anywhere else, you can define it right there in your js.erb template instead of some javascript file. If you do need it elsewhere, then make sure that your javascript file is loaded correctly. Reference the file in your layout, and then in the javascript file, just do something like this:

$(document).ready(function() {
  alert("I'm loaded!");
});

Comments

-1

Do it unobtrusively. tag your last image with a class (we'll use "button-last" for the sake of argument) and put it in the document.ready() event in a javascript behind the scenes, like application.js (this works if you're using jquery, you'd need to translate it to your framework of choice):

$(document).ready(function(){
  setLastID($("img.button-last").attr("id"));
});

2 Comments

Thank you, that is very useful... but I'd rather have access to the attribute @other_images rather than the id of the last image that loads (I've noticed these aren't always sequential). How can I call @other_images from here?
Also, it doesn't seem like the $(document).ready(function() can tell when I've loaded new partials into different divs on my page... but that's when I want the event to trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.