Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:13:53+00:00 2026-05-24T14:13:53+00:00

I have a big problem and no idea how to solve it. I need

  • 0

I have a big problem and no idea how to solve it.

I need to return values (mostly strings) to use them in various contexts. As an example, I need to check whether something is 0 or 1 in an if/else statement or insert a string in various html elements before parsing it via JavaScript into the DOM. Here is a simplified example of what I’m looking for:

<script>
    function output() {
        return "Hello world!";
    }

    function check() {
        return 3;
    }

    (function() {
        if(check() !== 5) {
            $(someElement).set("html", "<p>" + output() + "</p>");
        }
    })();
</script>

Now lets get into the headaching part: Before returning the value, I need to request it from a JSON context in an external file that loads the contents from the backend. I wrote a class that contains the Request.JSON MooTools class to make it easier to use with various requests:

<script>
    var backendCall = new Class({

        Implements: Options,

        options: {
            url: "gateway/?contentType=application/json"
        },

        initialize: function(options) {
            this.setOptions(options);

            if(!instanceOf(this.options.prms, Array)) {
                this.options.prms = [];
            }

            this.fire();
        },

        fire: function() {
            new Request.JSON({
                data: JSON.encode({
                    "methodName": this.options.req,
                    "parameters": this.options.prms,
                    "serviceName": this.options.srv
                }),
                onFailure: function() {
                    alert("JSON-Request failed.");
                },
                onSuccess: function(data) {
                    this.callback(data);
                }.bind(this),
                url: this.options.url
            }).send();
        },

        callback: function(data) {
            alert(data); // works
        }
    });


    function request(req, srv) {
        new backendCall({
            req: req,
            srv: srv
        });
    }


    window.addEvent("load", function() {
        /* is there a way to return the data string here?
            or is it impossible? */
        request("someValue", "demoTest");
    });
</script>

This works great when executing a hardcoded function inside the callback function that refers the JSON-Request output. But I need to make this part more variable because I want to use the output in different ways. Is there a way to let the callback just return the output and refer it to the parent request() function to let it return the value? Maybe with .pass(data)? Any ideas are welcome! Thank you so much! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:13:53+00:00Added an answer on May 24, 2026 at 2:13 pm

    well. XHR is async (ajax) so you can only get the value as a callback on the onComplete event.

    typically, you wait for the response and the onComplete can then go to the next item you are checking or finalise.

    eg, in pseudo code:

    form.addEvent("submit", function(e) {
        e.stop();
        var errors = false;
        var finalise = function() {
            if (!errors)
                this.send();
            else 
                alert("errors found!");
        }.bind(this);
    
        if (!field.length) errors = true;
        ... other sync checks.
        // does it exist?
        new Request.JSON({
            url: "/accountCheck/",
            method: "get",
            onComplete: function(data) {
                if (data.id) {
                     // already exists
                     errors = true;
                }
                // call finalise
                finalise();
            }
        }).send({user: field.value}); 
    }
    

    of course, you can set the async: false on the request object, which will block UI until it completes so the onComplete can set your var and the line immediately after the Request.send() will have it available to use.

    you can do a proper validator (or use a ready made one) which defines a ruleset and checks them in order, understanding async onComplete and having a stack of checks to work against, there are many examples of this and even mootools-more has a form-validator that’s pretty extensive (though I have not used it myself so cannot testify on how it deals with async checks)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys, i have a big problem with my jQuery Datepicker Script. I use
I have a big problem using TBXML . When I use: + (NSString*) textForElement:(TBXMLElement*)aXMLElement
I have a big problem with this code and I have no idea how
I have big problem when I am trying to deploy my app over clickonce.
I have a big problem. I want to extract text from html table that
I have a (big) problem that all of you that work with Webforms might
I have a big problem dealing with data I try to download in my
I have an big problem with an SQL Statement in Oracle. I want to
I am using gettext in my PHP code, but I have a big problem.
I have a very big problem. I am making a CRM (Costumer Relationship Management)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.