I put the following function in my application.js:
function test() {
alert("See Me")
}
classrooms/_new_small.html.haml:
:javascript
alert('test');
test();
In my application.html.haml I'm using the following:
= javascript_include_tag 'application'
I'm getting a test is not defined
error in my firebug console. Isn't the test function supposed to be available project-wide because its in the application.js file? Thanks
edit: I'm rendering the page via javascript. Would that affect it?
$('#test_container').html("<%= escape_javascript(render :partial => 'classrooms/new_small') %>");
Rails uses a technique called "Unobtrusive JavaScript" to handle attaching JavaScript to the DOM. This is generally considered to be a best-practice within the frontend community, but you may occasionally read tutorials that demonstrate other ways.
The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application. js file, which is just like the application.
Rules of ThumbCopy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder. Let the Rails asset pipeline combine them all in one minimized application. js file. List scripts in the app/assets/javascripts/application.
.js.erb files are for controller actions, such as create, when you want javascript to be executed when the action completes. For example, when you want to submit a form via ajax and want display an alert when everything is done, you would put the $('#business_submit'). click(...) in your application.
Assuming you are using asset pipeline & coffeescript: Don't write code in application.js
In order to keep clean structure, create a folder (eg: application
) in app/assets/javascripts
then require it in your application.js
//= require jquery
//= require jquery_ujs
//= require_tree ./application
Create a coffee file (eg: app/assets/javascripts/application/my_feature.js.coffee
@test = ->
alert('Hello world')
Coffee output:
(function() {
this.test = function() {
return alert('Hello world');
};
}).call(this);
It's preferable to use namespaces:
@myGreatFeature =
test: ->
alert('Hello world')
or according to the coffeescript FAQ, you could write
namespace = (target, name, block) ->
[target, name, block] = [(if typeof exports isnt 'undefined' then exports else window), arguments...] if arguments.length < 3
top = target
target = target[item] or= {} for item in name.split '.'
block target, top
namespace 'myGreatFeature', (exports) ->
exports.test = ->
alert('Hello world')
then you can call myGreatFeature.test()
everywhere
Simple solution, remove escape_javascript
, but it's dangerous see Why escape_javascript before rendering a partial? .
Currently you want to execute
$('#test_container').html("<script>
//<![CDATA[
alert('test');
test();
//]]>
</script>
which is converted into
$('#test_container').html("<script>\n //<![CDATA[\n alert(\'test\');\n test();\n //]]>\n<\/script>\n");
i will include project_wide.js in app/assets/javascripts/ and then tell application.js
to include my file .
//= require project_wide
Be sure to put the above line after
//= require jquery
//= require jquery_ujs
Now, whatever i put in app/assets/javascripts/project_wide.js
it will shamelessly appear in whole project except the files which are in public/
folder . In this file, i will put everything which i want to execute .
Currently,you are adding javascript directly inside classrooms/_new_small.html.haml
which is uncommon. Mostly , rails developer put such things in assets/javascripts/*.js
or call content_for :javascript_custom
block in views , which is added in layout file by yield :javascript_custom
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With