Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
what is the best way to manage external plugins (validval.js, datatables.js etc) in requirejs?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
frequent  
View profile  
 More options Apr 30 2012, 5:23 am
From: frequent <sven.fra...@stokkers.de>
Date: Mon, 30 Apr 2012 02:23:59 -0700 (PDT)
Local: Mon, Apr 30 2012 5:23 am
Subject: what is the best way to manage external plugins (validval.js, datatables.js etc) in requirejs?

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 :-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
frequent  
View profile  
 More options Apr 30 2012, 6:12 pm
From: frequent <sven.fra...@stokkers.de>
Date: Mon, 30 Apr 2012 15:12:08 -0700 (PDT)
Local: Mon, Apr 30 2012 6:12 pm
Subject: Re: what is the best way to manage external plugins (validval.js, datatables.js etc) in requirejs?

Ok, I think I found a way. I came up using

This article for a require on-demand autoloader
http://www.emanuel-kluge.de/tutorial/autoloader-funktionalitat-in-req...
- German sorry...
plus this great tutorial
http://verekia.com/requirejs/build-simple-client-side-mvc-app-require-js

My version uses no backbone or underscore. I'm loading core libs first and
then set listeners to pages, which invoke a service_plugin.js, which calls
the plugin.

If anyone wants to use the code - here is* MAIN.JS*:

*    require.config({
        baseUrl: "../js/",    
        paths: {    
            "app":   "",
**             // all plugin paths*
*            "jquery": "libs/jquery/jquery.min",****
             }
        });  *  

*    // needed for first page load  
    require(['order!jquery', 'order!...', 'app'],            
        function(jquery, ..., App){    
            App.start();
            });*

Inside the main *App.js* file:

      *define([], function(){
           var start = function() {
               // require ALL    
               require(['jquery', ...],function() {
                   // event listener
                   $('div:jqmData(role="page").basePage').on('pagecreate',
function() {
                      if ( $('form.val').length > 0 ) {
                        // here I'm calling validval plugin on all forms
with class val on the page being create
                       $(this).find('form.val').each( function() {
                            var that = $(this);                            
                            // call service (also called app!) to attach
validval and pass the form along as parameter
                            require(['services/validval/app'], function
(App) {                                
                                App.render({form: that });
                                });
                            });    
                         }                                                
                     });        
                   })
                  }
         return {"start":start};    
         });*    

This waits for pages to be shown, checks for form-elements on the page and
calls the render function inside the service for validval, also called *
app.js*:

    *  // load in the plugin
      define(['services/validval/app', 'services/validval/validval.min'],
function( app, validval ) {
            function render(parameters) {
                 parameters.form.validVal({
                     // run plugin once it's loaded
                     });                     **  
                  };        
           return { render:render };
        });*

I like this because it's using require.js, does not interfere with Jquery
Mobile and Coldfusion AND plugins are only being fetched if they are
required on a page.

Still curious... I have patched this together more or less. Is this a
viable approach?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »