Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put javascript files in the Phoenix framework

So I have a file foo.js which contains the following:

$( document ).ready(function() {
  alert("hello world");
});

and if I put it to the web/static/js folder then it doesn't get executed, but if I put it to the web/static/vendor folder then it does, so I wonder why it doesn't work from the js folder? And where should I put my js files? The vendor folder doesn't seem to be the right place ...

like image 694
NoDisplayName Avatar asked Jul 18 '15 07:07

NoDisplayName


People also ask

Where should I store JavaScript files?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body. JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.


2 Answers

As phoenixframework is using bruch.io as default. In it default configuration. There are two javascript folders

  1. web/static/js
  2. web/static/vendor

When you add .js files under /web/static/vendor these files will be put in non wrapped codebase. These files will undergo concatinations and other processes and brunch.io with other js files (which also include files under web/static/js) and then will be put it in priv/static/js/app.js

When you add .js files under web/static/js these files content will be put in wrapped codebase and then these file will undergo concatination with other brunch.io processes like previously mentioned. To reference to these file you need to use require() To require it first then you can use it.

I hope you understand the reasons here. I researched from https://github.com/brunch/brunch-guide/blob/master/content/en/chapter04-starting-from-scratch.md.

And these configuration can be overriden in the file brunch-config.js or brunch-config.coffee in the phoenixframework geterated folder content.

like image 118
Suracheth Chawla Avatar answered Oct 28 '22 17:10

Suracheth Chawla


It turns out that when you add new files to the js folder, you have to require it either in your html file or in app.js, that's one of the brunch's features

like image 38
NoDisplayName Avatar answered Oct 28 '22 19:10

NoDisplayName