Asynchronous calls

3
Hi all Mendix Gurus. How are you solving asynchronous calls in custom widgets? Please can you give some small example how to use defferedList? I am getting data like: var shiftLabels = []; var shiftTimes = []; mx.processor.get({ xpath: "//SitesDepartments.Site[SitesDepartments.Department_Site/SitesDepartments.Department/Staff.Rota_Department/Staff.Rota/id = "+rotaObject+"]", callback: function(companyList) { if(companyList.length == 0) { console.log('List of Companies is empty.'); } else { var company = companyList[0]; shiftLabels[0] = company.getAttribute("Shift1Label"); shiftLabels[1] = company.getAttribute("Shift2Label"); shiftLabels[2] = company.getAttribute("Shift3Label"); shiftTimes[0] = company.getAttribute("Shift1StartTime"); shiftTimes[1] = company.getAttribute("Shift2StartTime"); shiftTimes[2] = company.getAttribute("Shift3StartTime"); var shift1label = company.getAttribute("Shift1Label"); jQuery("#shift1label").val(shift1label); var shift2label = company.getAttribute("Shift2Label"); jQuery("#shift2label").val(shift2label); var shift3label = company.getAttribute("Shift3Label"); jQuery("#shift3label").val(shift3label); jQuery("#numberOfWorkingWeeks").val(company.getAttribute("WeeksInWorkingWeek")); jQuery("#numberOfWorkingDays").val(company.getAttribute("DaysInWorkingWeek")); } } }); After this call i am executing other mx.processor.get functions where I need to use those two arrays shiftLabels and shiftTimes. Can anybody help with concrete implementation? Regards, Lukas
asked
2 answers
7

Here you go,

dojo.provide("example.widget.SomeWidget");

mxui.widget.declare("example.widget.SomeWidget", {

    //
    // Skipping inheritance, inputargs, mixins, etc
    //

    shiftLabels : [],
    shiftTimes  : [],

    postCreate : function() {

        //
        // Sequencer will set the scope to 'this'
        // http://apidocs.mendix.com/client/lang.html#sequence
        //

        mendix.lang.sequence(this, [
           this.getSomeObjects,
           this.getSomeMoreObjects,
           this.doJQueryStuff,
           this.actRendered
            ]);
    },

    getSomeObjects : function(callback) {

        //
        //  Both mx.processor.get and dojo.forEach take a scope argument
        //  ('this'), which takes care of setting the proper scope. If you
        //  don't pass the scope object you have to set the scope yourself
        //  using dojo.hitch!
        //

        mx.processor.get({
                xpath : "//Module.Object",
                callback : function(objs) {
                    dojo.forEach(objs, function(obj) {
                            this.shiftLabels.push(obj.get("ShiftLabel"));
                        }, this);
                    callback();
                }
            }, this);
    },

    getSomeMoreObjects : function(callback) {
        mx.processor.get({
                xpath : "//Module.Object",
                callback : function(objs) {
                    dojo.forEach(objs, function(obj) {
                            this.shiftTimes.push(obj.get("ShiftTimes"));
                        }, this);
                    callback();
                }
            }, this);
    },

    doJQueryStuff : function(callback) {

        //
        // Do jQuery stuff, probably with this.shiftLabels and this.shiftTimes
        //

        callback();
    }

});
answered
0

Those arrays are already in the closure of your callback, so you can already use them.

Otherwise, you can bind them to your callback function using dojo.hitch:

...get({
  ...
  callback : dojo.hitch(this, function(shiftLabels, shiftTimes, companyList) { 
     ..your function..
  }, shiftLabels, shiftTimes);
}

For more information, just google a bit around to find out how javascript closures, dynamic scoping works.

answered