rjs partials

Hello Everybody,

To keep my code dry, I want to use a partial rjs from within another rjs. Is this possible and if so, how should I do this. If it's not possible, is there a way to cut up the code which I want to use in a call with rjs and How can this be done

Thanx in advance, Jeroen van Doorn

Cayce Balara schreef:

Jeroen van Doorn wrote:   

Hello Everybody,

To keep my code dry, I want to use a partial rjs from within another rjs. Is this possible and if so, how should I do this. If it's not possible, is there a way to cut up the code which I want to use in a call with rjs and How can this be done

Thanx in advance, Jeroen van Doorn      RJS is pure ruby code, so I would expect it can be done, but I'm a little confused as to exactly what you're attempting - can you put up a specific code example of how you think it should/might work? Will go a long way towards understanding your intent.

c.

Hi C,

What I want is something like the following

== _base_code.rjs page.update "base", "this is the base rjs" # a lot more code

== method1.rjs

page.update "example1", "SomeValue1" # a lot more code render :partial => "base_code", :file => "_base_code.rjs"

== method2.rjs

page.update "example2", "SomeValue2" # a lot more code render :partial => "base_code", :file => "_base_code.rjs"

The above is what I tried in a test app, but it doesn't work. The reason is that I have multiple function which call "base" with ajax after they are handled themselves. (these functions are ajax functions themselves). I also need to call "base" on it's own sometimes. But what I don't want anymore is the extra ajax call to "base" from the other functions. I want the needed rjs send in one call. Hope there's another solution or that I'm making some stupid mistake :wink:

Regards, Jeroen van Doorn

I think you want to use a technique I learned yesterday (thanks to Dave Hoover!) which relies on the use of nested singleton methods. It would look something like this using your example as a guide:

def some_action    # some model code or other setup code

   # the rjs stuff    update_page do |page|      page.method1      page.method2(some_arg)    end end

def update_page    render :update do |page|      def page.method1        replace_html "div1",                     :partial => "some_html"                     # might need :locals hash here      end

     def page.method2(some_arg)        replace_html "div2_#{some_arg}",                     :partial => "some_html"                     # again, you might need to explicitly                     # pass in some local variables depending                     # on how you setup your partial      end

     def page.base_code         # some more code      end

     yield(page)    end end

Apparently there is a special context setup when you do the "render :update do" call, so calling out to external methods upsets that context. Using the singleton methods keeps the context pure and will output the correct rjs.

This keeps your code looking pretty clean. I hope I didn't completely miss the point of your question...

cr

Oops, I forgot the call to #base_code in my example. Try this.

def some_action     # some model code or other setup code

    # the rjs stuff     update_page do |page|       page.method1       page.method2(some_arg)     end end

def update_page     render :update do |page|       def page.method1         replace_html "div1",                      :partial => "some_html"                      # might need :locals hash here         base_code # call the singleton object in this context       end

      def page.method2(some_arg)         replace_html "div2_#{some_arg}",                      :partial => "some_html"                      # again, you might need to explicitly                      # pass in some local variables depending                      # on how you setup your partial         base_code       end

      def page.base_code          # some more code       end

      yield(page)     end end

cremes.devlist@mac.com schreef:

Not really sure. Try running the code as given in a single file and then experiment with breaking it up. I imagine you can break some of it up except for the singleton methods.

cr