开发者

pass object to javascript_include _tag Rails

I want to put some jquery that I use for one page in a .js file to clean up my page.

However the jquery that is on the page gets passed an object to extract some information.

Here is the code:

function get_text(){
             var request_text;
             request_text = "Hello my name is " + $('#name').val() + ", and I would like to join the birthday surprise for <%= get_current_board.bp_name %>" +
        开发者_开发百科    " Could you please send me an invitation so that I can participate.\n\nThank you very much. \n\n" + $('#name').val();
            return request_text;              
        }

There is more to the code but this is the part that gets the object.

Can I wrap this up into a .js file and then just use a <%= javascript_include_tag %> even if it needs the get_current_board object?


Unless you run it through the erb processor, any erb in it will be completely ignored.

These days you can just create .js.erb files, although some people frown on generating JavaScript like that (I'm ambivalent; if it fits the need, go ahead :)

There are other options, including passing in an argument to get_text(), creating an argument object that all your JS can use, storing information in the DOM for JS to extract and use, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜