happypeter / haoduoshipin

好多视频
http://haoduoshipin.com
236 stars 88 forks source link

Javascript_include structure #38

Closed happypeter closed 11 years ago

happypeter commented 12 years ago

https://github.com/ruby-china/ruby-china is a great example

happypeter commented 12 years ago

NOTE!!!

<%= javascript_include_tag "app" %>
<%= yield :scripts %>

make sure app.js goes before local js, cause Jquery in app and local js depends on it.

Also, because everything is at the bottom, no need for document.ready() any more.

happypeter commented 12 years ago

Since I need to call rails helper in my jplayer js, so I can not just put it in episodes.js and use

<% content_for :scripts do %>
  <%= javascript_include_tag "episodes" %>
<% end %>

have to use:

<% content_for :scripts do %>
  <%= javascript_include_tag "episodes" %>
  <script>
    $("#jquery_jplayer_1").jPlayer({
      ready: function () {
        $(this).jPlayer("setMedia", {
          m4v: "<%= video_url @episode %>",
          poster: "http://media.happycasts.net/assets/episodes/stills/load.png"
        });
      },
      swfPath: "/assets/",
      supplied: "m4v",
      size: {
        width: "960px",
        height: "720px",
      }
    });
  </script>
<% end %>
happypeter commented 12 years ago

Maybe I'll use this http://requirejs.org/

happypeter commented 12 years ago

A js snippet for a partial:

  1. you can not put it in the partial itself, cause if will be rendered every time the partial is used, thus maybe u will see more than one of the same piece in a page. User content_for & javascript_include_tag in the partial is the same.
  2. you should not mix it into application.js, hard to maintain.

so my current solution is just create a editable_comment.js (for the partial named editable_comment.html.erb1) under app/assets/javascript, then require the js in application.js

happypeter commented 12 years ago

http://ruby-china.org/topics/7145