Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include JS in layout with rails helper #2215

Merged
merged 1 commit into from
May 24, 2013
Merged

Include JS in layout with rails helper #2215

merged 1 commit into from
May 24, 2013

Conversation

tank-bohr
Copy link

Use Rails helper to include javascript. Assets pipeline divides manifest into separate files if assets.debug is true

Use Rails helper to include javascript. Assets pipeline divides manifest into separate files if assets.debug is true
@seanlinsley
Copy link
Contributor

Could you elaborate more on what this solves?

@tank-bohr
Copy link
Author

Therу is a common rails interface to include assets in layout. This is javascript_include_tag
If you use this helper assets served as sepearate files in development environment and as one file in production (see rails guides)

For example:

In active_admin initializer:

config.register_javascript 'my_javascript'

my_javascript.js is a manifest file with require directives

//= require jquery.ui.widget.js
//= require jquery.iframe-transport.js
//= require jquery.fileupload.js
//= require blah-blah.js

I see one file in html in development instead of four. Inspite of config.assets.debug is true. My PS solves this issue. It's not critical but it's more predictable for rails developers

By the way, stylesheets are included with rails tag (https://github.com/gregbell/active_admin/blob/master/lib/active_admin/views/pages/base.rb#L27). It would be better to include js the same way for consistency

@seanlinsley
Copy link
Contributor

That doesn't quite work as I expected. With this change, all of these script tags showed up in the HTML:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/lib/namespace.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/components/jquery.aa.checkbox-toggler.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/components/jquery.aa.dropdown-menu.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/components/jquery.aa.popover.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/components/jquery.aa.table-checkbox-toggler.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/pages/application.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/pages/batch_actions.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/application.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin/base.js?body=1" type="text/javascript"></script>
<script src="/assets/active_admin.js?body=1" type="text/javascript"></script>

Do you have any idea what ?body=1 does? Visiting them in the browser:

/assets/active_admin.js        # returns the compiled version
/assets/active_admin.js?body=1 # returns an empty file

@tank-bohr
Copy link
Author

That doesn't quite work as I expected. With this change, all of these script tags showed up in the HTML

What do you expected? This is exactly what I want. This is extremly convenient in development mode. And it is very easy to turn off.

/assets/active_admin.js?body=1 # returns an empty file

It's ok. There is no any js code in active_admin.js. Only require directives. They trasnsforms to separate script tags and pure js code stays in manifest file. If you write some js in your active_admin.js you'll see it there

@seanlinsley
Copy link
Contributor

It doesn't work how I'd expect because:

  • it keeps and loads empty files like active_admin.js
  • except they're not empty. You can still access the full, compiled version simply by removing body=1 from the URL.

Do you know why the body=1 thing works like it does?

@seanlinsley
Copy link
Contributor

Thanks for abiding my laziness :]

Looks good. It'll be much easier to debug Javascript in the browser with this change. Thanks! 🐱

seanlinsley added a commit that referenced this pull request May 24, 2013
Include JS in layout with rails helper
@seanlinsley seanlinsley merged commit 3f58ea9 into activeadmin:master May 24, 2013
@tank-bohr
Copy link
Author

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants