0

Trying to learn to create a standalone game.js plugin. What is the best practice to call a function that depends on the result of two other separate functions? and sometimes call one function only. for example:

  1. function A{}
  2. function B{}
  3. function C{}

How to achieve this logic: result= C( B( A() ) );

in other pages; only need to call C();

I read this answer, but it didn't match my requirements.

for example; some block of jquery:

userID = 123;

$(function () {
    points = load_points(userID); // load points
    refresh(points); // refresh points' span and animate
    popup(points); // pop up user the points

    function load_points(userID) {
        // Read points from a PHP/MySQL page using Ajax then Return points
        // sample output: 100 point
        return result;
    }

    function refresh(p) {
        // update span value then animate
        $("#spanID").text(p);
        $("#spanID").slideDown(1000);
        return true; // return true after 1000 ms
    }

    function popup(msg) {
        // if #spanID value updated and animated
        // ; show overlay popup with points.
        // using this popup plugin here
        //http://dev.vast.com/jquery-popup-overlay/
        alert("You win " + msg+ " Points");
    }
});

The reason not wrapping everything in one function; is some functions are called several times.

For example:

Sometimes I want to refresh points in a page without showing the popup. Some other places I want to show the popup with different requirements.

I am concern about

  • The functions order. The success order related to functions' timing. i.e. I want to call a function after the result is available from the other function.
  • The speeds of the whole process.
wpcoder
  • 1,016
  • 11
  • 17
  • "The order" - the order will be the order you call it. `$("#spanID").slideDown(1000); // return afer 1000 ms` - that's now how `slideDown()` works, it will return immediately. – freedomn-m Oct 31 '17 at 13:17
  • @freedomn-m, I mean the success order related to functions' timing. Thank you, will update. – wpcoder Oct 31 '17 at 13:19
  • 1
    Unless you are using ajax or some other asynchronous code, eg waiting for a user to click something then all your code runs immediately. It's not clear from your examples if this is a misunderstanding of how jquery works (as implied by the comment) or if your code is doing something more completed (eg making an ajax call). – freedomn-m Oct 31 '17 at 13:27
  • 1
    My code is full of complicated and dynamic ajax calls, will create some jsfiddle and update to clarify my points further. – wpcoder Oct 31 '17 at 14:19
  • 1
    Have a look at promises and deferred https://api.jquery.com/promise/ / http://api.jquery.com/category/deferred-object/ – freedomn-m Oct 31 '17 at 16:30
  • Thank you @freedomn-m, interesting references, a lot of jQuery knowledge for a one day! – wpcoder Oct 31 '17 at 19:51

1 Answers1

1

On your first requirement, you want to be able to call popup(msg) in different ways depending on usage. You could perhaps structure your code differently and use the module pattern, having your current functions as private and exposing a couple of different functions:

$(document).ready(function() {
var module = (function() {

   var loadPoints = function(userID) {
     var root = 'https://jsonplaceholder.typicode.com';
     return $.ajax({
            url: root + '/posts/1',
            method: 'GET'
        });
     };

    var refresh = function(data) {
      alert(data.title)
    };

    var popup = function(msg) {
      alert(msg);
    };

    var refreshDataPoints = function(userID) {
     loadPoints(userID).then(refresh);
   };

   var getDataPointsPopup = function(userID) {
     loadPoints(userID).then(refresh).then(popup);
   };

   // Public API
   return {
     showPopup: popup,
     refreshDataPoints: refreshDataPoints,
     getDataPointsPopup: getDataPointsPopup
   };
 })();
 module.refreshDataPoints(1);
});

Then, depending on usage, you could call:

module.showPopup(msg); //basically a wrapper to your existing function

or:

module.getDataPointsPopup(userID);

The refreshDataPoints() & getDataPointsPopup() methods rely on promise and deferred objects, described in the answer you linked, I think this would satisfy the requirement that you're able to chain these methods and handle success & failure (the then() method accepts 2 parameters).

Ross P
  • 158
  • 2
  • 8
  • thank you so much for your answer @RossP ! Referencing: [how to use JS Module' answer](https://stackoverflow.com/a/12042471/7735285); I managed to test the proposed solution there. But when I try to run your code; initially I got function undefined. Then trying to manage; It looks working partially, i.e. When I call `module.showPopup("Test");` First it alerts `Test`, then chrome Dev. console (F12) shows `Cannot read property 'then' of undefined`. Excuse my weak knowledge in jQuery. – wpcoder Oct 31 '17 at 19:48
  • @wpcoder - I've updated the code sample so it's a bit more complete (I tested this in jsfiddle). Re the error you got - the *getDataPoints* method needs to return a promise. – Ross P Oct 31 '17 at 22:13