0

I have a jquery enabled javascript app that has a variable that is declared as a substantial hunk of code.

var Listings = Spine.Controller.sub({
    TPL: Handlebars.compile( $( '#entry-template' ).html() ),
    events: //stuff
    init: function() //stuff

    //a whole bunch of functions
});

I want to pull this hunk of code into a separate file (as it is about 300 lines long) and load it with jquery. However more importantly, I want to be able to use this code for two variables without resorting to copy-pasting.

0

1 Answer 1

2

Change your code to this:

function giveMeAName() {
    return Spine.Controller.sub({
        // massive code here
    });
}

Then you can include that file via jQuery as you would normally. After including, do this:

var Listings = giveMeAName();
var SomethingElse = giveMeAName();

You can also pass parameters, if the two uses of the code differ slightly.

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

4 Comments

My answer would have been the same if you weren't this fast. Have my upvote.
Sorry, @SoonDead, but I am known as The Sniper in some places ;)
This is what I tried before posting (I'm going to assume my load script is the issue now) because the function is undefined. $.getScript('filename.js');
Anything that relies on the result of an asynchronous function like getScript MUST be inside the success callback of that function.

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.