Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

whilst wp_enqueue_script seems to work it's not really elegant. I'm currently making a WP front end which uses MVC (backbone.js) and has literally 30+ seperate Model/View/Collection/Controller scripts and I keep adding to the number. I've used require.js a lot before and really like it, however it seems to completely break wordpresses logic.

Can anyone point me to a clean method of using a js scriptloader with wordpress? or is this a hopeless task?

Thanks

share|improve this question
1  
What's not elegant about it? Front end as in theme? What are you trying to achieve feature wise/performance wise that the wp_enqueue_script can not give you? –  userabuser Apr 28 '12 at 15:17
    
I'm trying to create a single page theme interfacing the JSON API with only the bare bones WP HTML generation as a fallback. I guess, I'm trying to delegate the responsibility for bootstrapping JS dependencies to the front end as opposed to the PHP as Ideally I'm only going to call a PHP page-render once then dynamically load additional JS deps when the app requires them. –  Alex Apr 28 '12 at 15:54

1 Answer 1

up vote 1 down vote accepted

Though WordPress already has a basic .js API (with register and enqueue script), I don't see why you cannot use it in conjunction with require.js, at least at the modular level.

From what I can tell (never used it) it only needs to be loaded, so you can,

wp_enqueue_script( 
     $handle, 
     '...folder../require.js',
      $deps,
      $ver,
      $in_footer 
);

Then use the recommended require folder structure and js code to take advantage of it. I believe that require.js is framework agnostic, even so it has it's own namespace function to avoid conflicts.

With that being said your still going to be limited to using it for your own code as opposed to plugins and themes you might be using.

Meaning, if a plugin or theme author does it the right way you can wp_dequeue_script the script and manage it with require.js, but what if they don't? There is not much you can do outside writing some sort of hacky plugin or manually changing the code.

For this reason using a js framework with wordpress is probably not worth the effect, unless your doing it from scratch and have control over the plugins and themes.

share|improve this answer
    
you mean only enqueue the require.js file and then (somehow, sorry my WP is not great) push in the actual js deps? –  Alex Apr 28 '12 at 16:15
    
just reading over here about it, codex.wordpress.org/Function_Reference/wp_enqueue_script seems as though this still means heaping a huge lump of js into the users' cache when it's perhaps unnecessary.. (using the deps will essentially link in those files when the page is rendered) –  Alex Apr 28 '12 at 16:18
    
Well you would just use require to load the js at the modular level, I don't see any other reason why one would use this if not doing it for that reason, I added to my answer. –  Wyck Apr 28 '12 at 16:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.