# Rjs partials

**URL:** <https://www.ruby-forum.com/t/rjs-partials/75504>\
**Category:** Rails\
**Created:** [November 10, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504 "2006-11-10T13:24:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 10, 2006, 1:24pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/1 "2006-11-10T13:24:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 10, 2006, 2:57pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/2 "2006-11-10T14:57:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 10, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/3 "2006-11-10T16:00:32Z")

</div>

Cayce B. schreef:

> > 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 😉

Regards,  
Jeroen van Doorn

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 10, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/4 "2006-11-10T16:42:44Z")

</div>

On Nov 10, 2006, at 8:59 AM, Jeroen van Doorn wrote:

> > > use in a call with rjs and How can this be done  
> > > long way towards understanding your intent.
> 
> # a lot more code
> 
> # a lot more code
> 
> 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 😉

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

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 20, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/5 "2006-11-20T14:46:23Z")

</div>

removed\_email\_address@domain.invalid schreef:

> > > > > Jeroen van Doorn
> > > 
> > > == method1.rjs
> > > 
> > > solution or
> 
> ```
> # the rjs stuff
> :partial => "some_html"
> base_code
> 
> ```
> 
> >

Hi CR,

Sorry for the late reply (I was moving to another place) … Thanxs,  
I’ll try this … do you have any idea if it’s possible to put the rjs  
code in seperate files?

Regards and thanxs again,  
Jeroen

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 10, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/6 "2006-11-10T20:13:15Z")

</div>

On Nov 10, 2006, at 9:40 AM, removed\_email\_address@domain.invalid wrote:

> > > > To keep my code dry, I want to use a partial rjs from within  
> > > > RJS is pure ruby code, so I would expect it can be done, but I’m a
> > 
> > render :partial =\> “base\_code”, :file =\> “\_base\_code.rjs”  
> > 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 😉

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

---

<div class="post-metadata">

**Author:** ![Jeroen\_van\_Doorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/49beb7/32.png) [@Jeroen\_van\_Doorn](https://www.ruby-forum.com/u/Jeroen_van_Doorn)\
**Post date:** [November 20, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/rjs-partials/75504/7 "2006-11-20T14:47:32Z")

</div>

On Nov 20, 2006, at 1:39 AM, Jeroen van Doorn wrote:

> > (snip)  
> > Hi CR,
> 
> Sorry for the late reply (I was moving to another place) … Thanxs,  
> I’ll try this … do you have any idea if it’s possible to put the rjs  
> code in seperate files?

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
