Javascript and partials

31 views
Skip to first unread message

Gm

unread,
Apr 22, 2015, 7:40:07 AM4/22/15
to rubyonra...@googlegroups.com
Hi,

I have a lot of javascript functions that can't be executed when I render a partial.
I mean. There are functions that area described inside a js file and when a partial that calls some functions from that file is rendered, nothing occurs.

How can I execute a javascript function when a partial is rendered ?

Thanks.

Walter Lee Davis

unread,
Apr 22, 2015, 8:10:49 AM4/22/15
to rubyonra...@googlegroups.com
Can you say a little more about how you are rendering these partials? Are you using remote: true to inject them into the page with JavaScript views, or are you rendering the whole page? If the latter, are you using Turbolinks? You may need to wrap your JavaScript in an event listener for the page:change event.

Walter



Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5085892d-7cd6-4a65-9b1e-1fc0221101d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gm

unread,
Apr 22, 2015, 8:18:33 AM4/22/15
to rubyonra...@googlegroups.com
Hi,

I have this simple function inside a js file:

$('input:button').click(function() {
   $('#panel').removeClass('hidden');
});

When I render a partial with a button inside:

<input type="button" value="Save">

The click function inside js.file is not executed.
I think it's because the js file is "included" in the main page, and inside the partial that js is not available.

Walter Lee Davis

unread,
Apr 22, 2015, 8:29:56 AM4/22/15
to rubyonra...@googlegroups.com


On Apr 22, 2015, at 8:18 AM, Gm <javap...@gmail.com> wrote:

Hi,

I have this simple function inside a js file:

$('input:button').click(function() {
   $('#panel').removeClass('hidden');
});

When I render a partial with a button inside:

<input type="button" value="Save">

The click function inside js.file is not executed.
I think it's because the js file is "included" in the main page, and inside the partial that js is not available.

Almost. It's because that js has already 'seen' the page, and your button wasn't in it when it 'looked', so it didn't register the click handler on it. Take a look at the deferred listener functions, using the on() method in jQuery or Prototype. Those let you set up a listener at a higher level (like the page, or just a parent to the element in your partial), and then it doesn't matter if the button was  injected after the fact or not.

In Prototype: 

    document.on('click', 'input[type="button"]', function(evt, elm){
      // do something
    });

Walter


On Wednesday, April 22, 2015 at 9:10:49 AM UTC-3, Walter Lee Davis wrote:


On Apr 22, 2015, at 7:40 AM, Gm <javap...@gmail.com> wrote:

Hi,

I have a lot of javascript functions that can't be executed when I render a partial.
I mean. There are functions that area described inside a js file and when a partial that calls some functions from that file is rendered, nothing occurs.

How can I execute a javascript function when a partial is rendered ?

Can you say a little more about how you are rendering these partials? Are you using remote: true to inject them into the page with JavaScript views, or are you rendering the whole page? If the latter, are you using Turbolinks? You may need to wrap your JavaScript in an event listener for the page:change event.

Walter



Thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5085892d-7cd6-4a65-9b1e-1fc0221101d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Gm

unread,
Apr 22, 2015, 8:54:04 AM4/22/15
to rubyonra...@googlegroups.com
Walter, thanks for your help.
I'll try to modify my code to this approach.

Thank you.

Gm

unread,
Apr 22, 2015, 2:51:58 PM4/22/15
to rubyonra...@googlegroups.com
Thank you sir.
Works like a charm.
Reply all
Reply to author
Forward
0 new messages