te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>How do you limit CoffeeScript (or JavaScript) execution to a particular controller and action in Rails 3.1? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do you limit CoffeeScript (or JavaScript) execution to a particular controller and action in Rails 3.1? - Stack Overflow

programmeradmin2浏览0评论

The new Rails 3.1 asset pipeline is really nice, but since all CoffeeScript (or JavaScript) files get melded down into a single file that is included in every page, it raises this question:

How do I limit the execution of my script to a particular controller or action? Is there a way within my CoffeeScript to know which controller and action was used during the request so that I can put conditional statements in my script?

Or am I approaching this the wrong way altogether?

The new Rails 3.1 asset pipeline is really nice, but since all CoffeeScript (or JavaScript) files get melded down into a single file that is included in every page, it raises this question:

How do I limit the execution of my script to a particular controller or action? Is there a way within my CoffeeScript to know which controller and action was used during the request so that I can put conditional statements in my script?

Or am I approaching this the wrong way altogether?

Share Improve this question asked May 26, 2011 at 11:27 adamjcooperadamjcooper 1,1411 gold badge12 silver badges24 bronze badges 2
  • 1 Possible duplicate of stackoverflow./questions/6133235/…; see my answer there. – Trevor Burnham Commented May 26, 2011 at 16:10
  • Thanks Trevor; making script conditional on the presence of an element or a particular CSS class on the body tag seems like a nice approach. – adamjcooper Commented May 26, 2011 at 18:54
Add a ment  | 

5 Answers 5

Reset to default 9

Trevor Burnham answers this question nicely here: How do I associate a CoffeeScript file with a view?

He says:

There are two mon approaches:

  1. Make behavior conditional on the presence of a particular element. For instance, code to run a signup sheet should be prefaced with something like

    if $('#signup').length > 0

  2. Make behavior conditional on a class on the body element. You can set the body class using ERB. This is often desirable for stylesheets as well. The code would be something like

    if $('body').hasClass 'user'

And if you're interested in CoffeeScript, Trevor is working on a book that looks to be very good: http://pragprog./titles/tbcoffee/coffeescript

One way to restrict coffeescript to a particular view is to make a custom sprockets file for the javascript in question, similar in format to application.js. Say you call it extras.js.

//= require my_code.js.coffee

Then use javascript_include_tag "extras" to include that code in the views you want, either by making a custom layout for those views, or by using content_for()

BTW, your question stated that the rails pipeline forces you to put all your js assets in one file. That's not true. That's efficient often to avoid multiple round trips, but you can have multiple sprocket files.

Why not to put the javascript for the particular controller as a view on this controller (as they correspond there if are so specific)?

If they are general enaugh you can mark your view with classes, ids or data (html5) and make your javascript look for that (so you can reuse your code).

what i normally do is to have a yield :js under the javascripts in my layout and when I need a specific script it, I load it directly from my view with:

content_for :js do
    javascript_include_tag "myscript"
end

If you are using the gon gem for your vars in coffee script you can use this pattern:

Put a flag for every action in the controller:

  def index
    @gps_coords = GpsCoord.all

    # Flag for the CoffeeScript to decide which part to run
    gon.index = true;
  end

  def show
    @gps_coord = GpsCoord.find(params[:id])

    gon.lat = @gps_coord.latitude
    gon.lon = @gps_coord.longitude

    gon.show = true;
  end

In the correlating coffee script use those flags to distiguish between the both actions:

  # index action?
  if gon.index? 
    first_coord = gon.gps_coords[0]
    map.setView([first_coord.latitude, first_coord.longitude], 15);

  # show action?
  if gon.show?
    map.setView([gon.lat, gon.lon], 15);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论