Ruby on Rails Thursday, January 1, 2015

Generally you make a core set of javascript available site-wide, however you don't execute the javascript site-wide (so I would move the actually call you make to the jquery plugin into a place where it is executed only on pages you want it executed).

Loading via the asset pipeline in a production, correctly concatenated gzipped,  especially if you set up a CDN correctly, is exponentially faster than loading every Javascript library one-by-one in the browser. That's why the Asset Pipeline was invented. 


After adding it to the application.js file. It changed backgrounds for 
the entire application. I wish there was a way to constrain this 
function to fire only on the pages that I need to be on
-- 


There's lots of ways to do that. 



I got it working. I had to remove the require statement from the 
application.js and add the file to assets pipeline directly. I also 
created a separate javascript file for the home page only. Now, it only 
shows on one page!


No this is not the correct way to do it and probably won't work on your live website. Unless it is very large, I would recommend you loading the jquery file itself on every page but the restricting the Javascript that actually execute that particular method to only the pages you want it to fire on. There are several good patterns for doing that. I explored one of them in this blog post of mine from a few months ago:


(See the section "CSS Scoping Pattern #2") 


Finally, what you've basically done is said, "Instead of learning the Asset Pipeline, I'm just going do re-invent the wheel and do things my own way." 

This strikes me as a behavior pattern that will be challenging for you as to learn more Rails. 





On Jan 1, 2015, at 12:58 PM, David Williams <lists@ruby-forum.com> wrote:

Jason Fb wrote in post #1165839:
3) you probably don't want to load that file from your layout, you
probably want to load the application.js as instructed by the Gem
instructions

Jason Fleetwood-Boldt
tech@datatravels.com
http://www.jasonfleetwoodboldt.com/writing

All material © Jason Fleetwood-Boldt 2014. Public conversations may be
turned into blog posts (original poster information will be made
anonymous). Email jason@datatravels.com with questions/concerns about
this.


I have a question, will placing this function inside of the 
application.js override the layout throughout the entire app? Or can I 
call the backstretch.js for different pages? I'm really using this gem 
just to get the carousel background effect for the index landing page.

The instructions says to bundle install and add //= require statement to 
the application.js. Which is what I did. I may use the gem again for 
pages other than the index to call backgrounds with one line of code. My 
original setup involved using css to cover the entire background with a 
fixed image.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/babdb2a70af6242e32cd8cf803bb1a79%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


----

Jason Fleetwood-Boldt
tech@datatravels.com
http://www.jasonfleetwoodboldt.com/writing

No comments:

Post a Comment