My Sencha Touch utilities (JSON, error message, dump object, REST URLs)

The following code shows my current Sencha Touch utilities. Much of it comes from a Packt book I can’t remember the name of, but several of the JavaScript functions are my own:

Ext.define('VP.util.Util', {

    // most of this code is copied from a Packt book

    statics : {
        decodeJSON : function (text) {
            var result = Ext.JSON.decode(text, true);
            if (!result) {
                result = {};
                result.success = false;
                result.msg = text;
            }
            return result;
        },

        showErrorMsg: function (text) {
            // Ext.Msg.alert('Error!', text);
            Ext.Msg.show({
                title:'Error!',
                msg: text,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK
            });
        },

        dumpObject: function(obj) {
            var output, property;
            for (property in obj) {
                output += property + ': ' + obj[property] + '; ';
            }
            console.log(output);
        },

        // `method` is typically 'GET' or 'POST'
        callRestUrl: function(theUrl, theMethod) {
            Ext.Ajax.request({
                url: theUrl,
                method: theMethod,
                success: function(conn, response, options, eOpts) {
                    var result = VP.util.Util.decodeJSON(conn.responseText);
                    if (result.success) {
                        // ignore
                    } else {
                        VP.util.Util.showErrorMsg(result.msg);
                    }
                },
                failure: function(conn, response, options, eOpts) {
                    // TODO get the 'msg' from the json and display it
                    VP.util.Util.showErrorMsg(conn.responseText);
                }
            });
        }
    }
});

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.