I have a site which is using a number of external jquery plugins
(datatables, validval, flexslider etc...). Not every page is using every
plugin, so I thought this would be a nice idea to try set this up with
requirejs (my first attempt with require, too).
The site is running Jquery-Mobile, which I want to keep handling
navigation. Contents are managed by Coldfusion, so I would only want to use
requirejs to managed plugins and load-on-demand.
While I could get it to work loading in everything upfront, I'm not able to
do get the following to work:
Inside *main.js*:
* require.config({
baseUrl: "../js/",
paths: {
"app": "",
"jquery": "libs/jquery/jquery.min",
"jqm": "libs/jquery-mobile/jquery-mobile.min",
"flexslider": "services/flexslider/flexslider.min",
}
});
require(['order!jquery', 'jqm', 'app'], function(jquery, App){
App.start();
}); *
Inside my *App.js* it gets tricky. I'm using Jquery Mobile so I'm listening
for specific events (pagechange, submit, etc.), and am checking for
elements that require plugin work and *only then* want to pull in the
specific plugin and service script. To do this I put all my plugins inside
folders in a *service *folder and now want to call them using
plugin-specific serv_PLUGIN.js files (which initialize the plugin, trigger
the validation and can pass pack info to app.js).
It should look like this:
*define(['jquery', 'jqm', 'App' ], function ($, mobile, App) {
var start = function() {
$(document).on('pagecreate','div:jqmData(role="page")',
function() {
console.log("detected event")
if ( $('.flexslider').length > 0 ) {
require(['flexslider', 'services/serv_flexslider'],
function(flexit){
var flexInit = serv_flexslider(
$(this).find('.flexslider') );
// return - nothing here
})
}
});*
*}
return {"start":start};*
Unfortunately this breaks because I cannot reference my serv_PLUGIN or
plugin file inside my services folder.
*Two questions:*
Does an event-oriented structure pictured above, where putting listeners in
a central *app.js* file (propably should be a controller.js), which on
events pass parameters to *services.js*, which fire the plugins to do the
heavy work make sense? I know require can do a lot more and I should
probably take a look at backbone, too. But I'm running a lot of things in
Jquery Mobile (panel navigation, etc.) and want to have a working version
before I'm trying to improve on it.
And - is there way I can get the above to work = can I reference folders by
dot-notation to call a service or plugin from app.js?
Thanks for inputs! Looking forward to requiring.js more :-)