RJS: how to generate different code? plugins?

Hi everyone :slight_smile:

If I wanted to make an RJS template generate something, how would I go
about
doing that? Like, right now you have page.insert_html, etc., but let’s
say
I wanted to define my_page.insert_html, and I want it to generate
different
code. How would I do that?

Thanks!
Sean

View this message in context:
http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

duma wrote:

Hi everyone :slight_smile:

If I wanted to make an RJS template generate something, how would I go
about
doing that? Like, right now you have page.insert_html, etc., but let’s
say
I wanted to define my_page.insert_html, and I want it to generate
different
code. How would I do that?

Thanks!
Sean

View this message in context:
http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

It seems that that page object is a:

ActionView::Helpers::PrototypeHelper::JavaScriptGenerator

So you should be able to extend that. I just tested this out and it
seems to work fine.

class ActionView::Helpers::PrototypeHelper::JavaScriptGenerator
def foo(m)
call “alert”, m
#same as: call “alert(’#{m}’)”
end
end

then you can do:

render(:update) { |page| page.foo(‘Foo!’) }
#=> “alert(‘foo’)”

If you want to override one the original methods, you can use
alias_method_chain.

class ActionView::Helpers::PrototypeHelper::JavaScriptGenerator
def alert_with_foo(message)
alert_without_foo("#{message} Foo!")
end
alias_method_chain :alert, :foo
end

render(:update) { |page| page.alert(‘Hi’) }
#=> “alert(‘Hi Foo!’)”

Nice!! Thank you so much!! :slight_smile:

Paul Johnson-18 wrote:

different

end
def alert_with_foo(message)


View this message in context:
http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8230019
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs