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.

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.