使用简单的 javascript 并通过 Rails3 和 UJS 传递参数
我看过关于不引人注目的 javascript 的 Railscasts 并阅读了大量教程,但是关于 Rails3 中不引人注目的 javascript 的两件事仍然让我感到困惑:
- 简单 javascript 的端点(隐藏一些元素,添加 CSS 类,...)
- 将参数传递给 JS
让我澄清一下这与示例代码。我想创建一个链接,淡出一些 id 为“sample”的元素:
link_to 'Fade sample', url, :remote => true
url 应该是什么,以便它可以执行 JS?它是否应该是控制器中名为“javascript”的新操作,以便它可以访问 javascript.js.erb 中的 JS,其中包含:
$('#sample').fadeOut();
另外,关于 ujs 的第二个问题与将参数传递给 JS 有关(在本例中为超时)。我可以写:
link_to 'Fade sample', url, :data-timeout => 1500, :remote => true
但不知道如何在javascript中访问数据超时。
我正在使用 Rails 3.0.5、JQuery 1.5.2 和 jquery-ujs。
I have watched railscasts about unobtrusive javascript and read numerous tutorials, but two things about unobtrusive javascript in Rails3 still confuses me:
- End-point for simple javascript (hiding some elements, adding CSS class, ...)
- Passing arguments to JS
Let me clarify this with sample code. I want to make a link which fades out some element with id "sample":
link_to 'Fade sample', url, :remote => true
What should be the url so it can execute JS? Should it be new action in controller named for example 'javascript' so it can access JS in javascript.js.erb which contains:
$('#sample').fadeOut();
Also, second question about ujs is related with passing arguments to JS (timeout, for this example). I can write:
link_to 'Fade sample', url, :data-timeout => 1500, :remote => true
but don't know how to access data-timeout in javascript.
I am using Rails 3.0.5, JQuery 1.5.2 and jquery-ujs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试一一回答。
link_to
中的网址。它是应用程序中任何资源的 url 哈希值。假设您在routes.rb中指定了resources :foos
,它将为CRUD操作创建6个url。您可以通过new_foo_url
、foo_url(@foo)
或foos_url
等方法将它们传递给 url。即使您可以手动传递哈希,如{:controller=>:foos_controller, :action=>:index}
,也可以将多个参数传递给哈希,如{:controller= >:foos_controller, :action=>:index, :param1=>"value1", :param2=>"value2"}
.您还可以在link_to
中传递字符串 url,例如foos/new
、foos/1
等您可以使用 jQuery 轻松访问自定义标签属性。例如,您想使用
id="link"
访问锚点,可以$('#link').attr("data-timeout")
。如果您不知道 id 到底是什么,但知道它有一个属性,您可以调用$("a[data-timeout]").first().attr('data-timeout')< /code>
I would try to answer one by one.
url in
link_to
. It would be a url hash for any resource in the application. Lets suppose you have specifiedresources :foos
in the routes.rb, it will create 6 urls for CRUD operation. You can pass these to the urls by the methods likenew_foo_url
,foo_url(@foo)
orfoos_url
. Even you can pass a hash manually like{:controller=>:foos_controller, :action=>:index}
, also you can pass multiple parameters to the hash like{:controller=>:foos_controller, :action=>:index, :param1=>"value1", :param2=>"value2"}
. Also you can pass strings urls likefoos/new
,foos/1
etcYou can access custom tag attributes with jQuery very easily. for example you want to access anchor with
id="link"
, you can$('#link').attr("data-timeout")
. If you don't know what exactly the id is, but you know it have an attribute you can call$("a[data-timeout]").first().attr('data-timeout')