4

this might be an issue of, I don't even know what to look for, thereby repeating a question. If so please link correct page and I will scurry to the corner in shame! Thanks in advance!

BEFORE: I was just running a game with an index.html and two file_name.js files, and pulling them in the head along with jQuery.

In my old HTML file:(This worked) I had a game.js file (doing doc ready, on click and keyup commands) and a gamelogic.js file (managing all the game logic)

My code from my OLD index.html file

<script src="https://code.jquery.com/jquery........."></script>
<script src="js/gamelogic.js"></script>
<script src="js/game.js"></script>

Now:
(Transferring a game over to rails) I'm in a Rails 4 app, scaffolded a game Model.

Created two new .js files with similar names in the app/assets/javascripts file

The problem: I know both files are being loaded, as I can console.log("foo") from both. And I know all my functions work, as when all code is combined to one file, all feature function.

I would like to keep them as two different files as the doc ready section is already kinda long and so is the logic section.

Below is the goal, call a logic function inside of the main game file.

File game.js

$(document).ready(function() {logicFunction();};

File gamelogic.js

function logicFunction(){console.log("BAR")}

Just a note: this is not for production or best practices, this is to learn the ins and outs of rails, and what things can and can't do. I doubt my code "should" be split, however I am curious if it's even possible. I will also we converting to coffee once I get functionality.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TallOrderDev
  • 469
  • 4
  • 15

1 Answers1

2

A much detailed explanation on your above question is in the following link. please check through

How to include js file in another js file?

Community
  • 1
  • 1
Shabini Rajadas
  • 731
  • 8
  • 15
  • Thanks for the response, As this is in a rails file I'm having some issues with the idea that is presented in the link. (which may be due to my ignorance!) however I know both files are being loaded into the page as I'm seeing the console get logs from both. However I'm not able to call functions from the other, like I was in my very simple app example. If you have any insight on converting the link to rails app context I'd absolutely love to hear! Thank You either way! _Edit: Method in link would cause js files to load twice, I think._ – TallOrderDev Sep 23 '16 at 10:10
  • How do you add the js files inside application.js? I meant the sequence. Do you load the gamelogic.js file first and then the game.js? – Shabini Rajadas Sep 24 '16 at 08:02
  • Great question and I'm not sure since it's rails it's auto added you do not type it into the head of the layout file. All files in the App/assets/javascript file are loaded, like every rails app. – TallOrderDev Sep 26 '16 at 01:49
  • Can you please add the sequence inside the application.js for just these two files? //require gamelogic //require game //require . inside the application.js, just to override the default sequence taken by the rails application. – Shabini Rajadas Sep 26 '16 at 06:11