RJS doesn't execute raw Javascript?

I have an RJS script that renders a partial with some javacript that
runs a calendar script. Basically:

var cal_#{id} = new CalendarPopup();
cal_#{id}.setReturnFunction(“setMultipleCalDateValues_#{id}”);

But this doesn’t seem to get executed within the browser. In fact, if I
view the “generated source” in Firefox the JS portion doesn’t even show
up. If I use “page << “[javascript source]”” it doesn’t work either.
If I render the partial from within a view it works fine. If I call the
RJS directly I can see the javascript within a try block.

Can anyone help me understand what is going on?

RJS is Ruby code that generates Javascript. So just typing out straight
Javascript gets interpreted as Ruby, but if it’s just a string it gets
discarded as an unused local variable.

page << “javascript” is what you want. Can we see the entirety of the
RJS
file?

Jason

Jason R. wrote:

RJS is Ruby code that generates Javascript. So just typing out straight
Javascript gets interpreted as Ruby, but if it’s just a string it gets
discarded as an unused local variable.

page << “javascript” is what you want. Can we see the entirety of the
RJS
file?

Jason

page << “var cal_#{@entry_number} = new CalendarPopup();
cal_#{@entry_number}.setReturnFunction(“setMultipleCalDateValues_#{@entry_number}”);”
page.insert_html :before, ‘total_row’,:partial => ‘entry_line’, :locals
=> {:entry_number => @entry_number, :time_entry => @time_entry}
page.replace ‘add_link’, :partial => ‘add_link’, :object =>
@entry_number
page.visual_effect :highlight, ‘total_row’, :duration => 3

That’s the RJS file. But like I said. Even if that javascript code is
in the ‘entry_line’ partial, it still doesn’t seem to get executed.

Do you have Firebug installed? There’s probably an error in the code
somewhere that’s halting execution.

Jason

Jason R. wrote:

Do you have Firebug installed? There’s probably an error in the code
somewhere that’s halting execution.

Jason

I do now :slight_smile:

The console doesn’t show any errors. Is there somewhere else I should
look?

another update:

The error I’m actually getting is that cal_[id] (the one that I’m
creating with ‘var cal_[id] = new CalendarPopup();’) is not defined when
I click on a image that references cal_[id] in it’s onClick. Is
variable created, but in a different context?

update:

If I replace my javascript with “alert(“test”);” I get the alert. So
I think it’s trying to execute but is not for some reason. Since the
javascript works when called as a partial within a view, could it have
something to do with how it is serialized for the ajax response?

Ryan Belcher wrote:

another update:

The error I’m actually getting is that cal_[id]

I don’t know what your general issue is, but ‘cal_[id]’ is not a valid
HTML id code. They can’t have a [] in them. That’s why Rails emits
tags with dual identifiers; a name like ‘cal_[id]’, and an id like
‘cal__id’.

So if you really mean an id there…


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!

Phlip wrote:

Ryan Belcher wrote:

another update:

The error I’m actually getting is that cal_[id]

I don’t know what your general issue is, but ‘cal_[id]’ is not a valid
HTML id code. They can’t have a [] in them. That’s why Rails emits
tags with dual identifiers; a name like ‘cal_[id]’, and an id like
‘cal__id’.

So if you really mean an id there…


Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!

Yeah, I meant an id as in cal_2 or cal_3.

Doing some more testing I figured out than I can alert cal_[id] in the
javascript in the RJS file and it sees it as an object. So the variable
is getting defined, but when I reference it in an onClick on the page
it’s not defined. Which leads me to believe it’s getting
defined/executed in a different context than the main window.

Is that supposed to be the case?

It’s very possible. I can’t remember what scoping Javascript does for
different browsers, but your RJS code is executed as such:

try {
[ your code here ]
} catch {
alert("[your code here again]"); // in development mode
}

So you’re defining the variables in a try/catch block, which might be
causing scoping issues.

Jason

Jason R. wrote:

It’s very possible. I can’t remember what scoping Javascript does for
different browsers, but your RJS code is executed as such:

try {
[ your code here ]
} catch {
alert("[your code here again]"); // in development mode
}

So you’re defining the variables in a try/catch block, which might be
causing scoping issues.

Jason

That’s it. I was using the var keyword when declaring cal_[id]. If I
remove var then I can access the variable in the rest of the page.

Thanks to everyone for helping me work through this.

Ryan Belcher wrote:

Jason R. wrote:

It’s very possible. I can’t remember what scoping Javascript does for
different browsers, but your RJS code is executed as such:

try {
[ your code here ]
} catch {
alert("[your code here again]"); // in development mode
}

So you’re defining the variables in a try/catch block, which might be
causing scoping issues.

Jason

That’s it. I was using the var keyword when declaring cal_[id]. If I
remove var then I can access the variable in the rest of the page.

Thanks to everyone for helping me work through this.

Also for anyone else interested in this problem:

I was also defining a function by name “function fname_1 (){}” and I
could not access this function elsewhere in the document. I needed to
assign the function to a variable “fname_1 = function () {}”

page << “var cal_#{@entry_number} = new CalendarPopup();
cal_#{@entry_number}.setReturnFunction(“setMultipleCalDateValues_#{@entry_number}”);”
page.insert_html :before, ‘total_row’,:partial => ‘entry_line’, :locals
=> {:entry_number => @entry_number, :time_entry => @time_entry}
page.replace ‘add_link’, :partial => ‘add_link’, :object =>
@entry_number
page.visual_effect :highlight, ‘total_row’, :duration => 3

That’s the RJS file. But like I said. Even if that javascript code is
in the ‘entry_line’ partial, it still doesn’t seem to get executed.

I encountered this problem before and it seems that escaping the double
quotes is the problem. must be a bug in rjs or something.