1. 2011
    Jul
    6

    Remote Calling… What the Heck?

    Posted By



    This idea has been around for a while and is really the ability to call a previously defined method/function. A lot of programming languages already have this functionally inbuilt in their core functionality. An important thing to note: remote calling is not suitable for everything you are doing. It only suitable for instances when you are using functions or methods that you WILL continually reuse throughout the web application for instance some kind of user activity reporting or storing user cookie information.

    
    
    var alertThis = function() {
        alert("This is an alert message.");
    };
    
    

    … On a later instance

    
    
    var checker = "allow";
    if(checker == "allow") {
         alertThis();
    };
    
    

    With JavaScript you can use this simple principle and take it to the next level using remote calling.

    JQuery:

    
    
    $.remoteLoader = function () {
       $(document).ready(function() {
              $.remoteCalling();
       });
    };
    
    $.remoteMethods = function () {
       if(typeof(firstFunction) == "function"){  firstFunction(); }
       if(typeof(dispathGenericA) == "function"){ seconfFunction(); }
    };
    
    $.firstFunction =  function () {
            alert('This is first Function.');
     };
    $.secondFunction: function () {
            alert('This is second Function.');
    };
    
    

    First script on header

    
    
    $.remoteLoader();
    
    

    Mootools:

    
    
    var RemoteLoaders = new Class({
        initialize: function () {
        },
        setup: function () {
           instance = this;
           window.addEvent('domready', function () {
                instance.remoteMethods();
            });
        },
        remoteMethods: function () {
            if (typeof(firstfunction) == 'function') { firstFunction(); }
            if (typeof(secondFunction) == 'function') { secondFunction(); }
        },
        firstFunction: function () {
            alert('This is first function.');
        },
        secondFunction: function () {
            alert('This is second function.');
        }
    });
    
    var remoteLoader = new RemoteLoaders();
    
    

    First script on header

    
    
    remoteLoader.setup();
    
    

    Because of the placement of the remoteLoader on top of everything,
    its function/methods get called first before any other functions/methods.

    Benefits:

    You can control which sequence functions/methods get called by the event handler and set levels of precedence for each function to be remotely called.Say we want to implement the functions/methods defined above, so that
    the second function gets implemented first and the first function
    gets implemented later.

    JQuery:

    
    
    alert(‘This is a simple alert.’);
    
    var firstFunction = function() {
           $.secondFunction();
    };
    
    var secondFunction = function() {
           $.firstFunction();
    };
    
    

    Mootools:

    
    
    alert('This is a simple alert.');
    
    var firstFunction = function() {
           remoteLoader.secondFunction();
    };
    
    var secondFunction = function() {
           remoteLoader.firstFunction();
    };
    
    

    Now you have the alert “This is second function” first,
    then alert “This is first function” second
    and “This is a simple alert” last.

    It can be viewed as abstract methods in which its definitions are adaptive depending on its environment.

    Here you have the freedom to completely redefine a function
    or method. Say we have 2 pages that require firstFunction to
    be implemented in page 1 as is, but requires a slightly
    different implementation on page 2.

    JQuery:

    PAGE 1:

    
    
    var firstFunction = function() {
           $.firstFunction ();
    };
    
    

    PAGE 2:

    
    
    var firstFunction = function() {
           $.firstFunction ();
          alert(‘This has been added to firstFunction’);
    };
    
    

    Motools:

    PAGE 1:

    
    
    var firstFunction = function() {
           remoteLoader.firstFunction ();
    };
    
    

    PAGE 2:

    
    
    var firstFunction = function() {
           remoteLoader.firstFunction ();
          alert(‘This has been added to firstFunction’);
    };
    
    

    Now we have the same function/method with
    different implementations on different pages.

    Disadvantages:

    These functions cannot be used as a helper function/method and can only be implemented once on a page.


  2. About Emeka Echeruo

    Emeka Echeruo

    I love sports, football which I refuse to call soccer, and the outdoor especially walks in park. Software development is my passion, there is a beauty in creating something out of nothing but algebra that ends up becomes a part of a persons daily life. I love kids, dogs, nightlife and art because it finds you and moves you emotionally!

  3. Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.