How to properly work with jQuery in Rails 3.1 asset pipeline?

前端 未结 2 941
名媛妹妹
名媛妹妹 2021-02-19 02:12

I\'m working on a hobby app and using some jQuery. The results are fine at the moment, but I\'m a jQuery noob and I assume that there are some significant improvements that I ca

2条回答
  •  情歌与酒
    2021-02-19 03:14

    1) Compilation: Rails assets pipeline just combines all javascript files in one big file.

    2) jquery is only loaded once, you are having multiple $(document).ready functions, but that is not a problem

    3) Rails does not do anything with the call, and jQuery can safely handle more of those blocks per page.

    4) You call it a model-specific .js, I would rather call it controller-specific. You group functionality together that belongs together. Whether the thing that ties them together is a controller or a model really does not matter. We split our js up in separate files to make it more manageable.

    5) In development the assets are compiled on every request, in production it is done only once. Also in production it could be minified and compressed.

    Hope this helps.

提交回复
热议问题