Showing posts with label Dojo. Show all posts
Showing posts with label Dojo. Show all posts

Sunday, October 14, 2012

AJAX error handling in DOJO



In Order to return an Ajax error from the server, you need to set the response status to 500, For example if you are working with Java Servlets, your Java Servlet will return the error as follows:

public class AjaxServlet extends HttpServlet {
protected void doPost (HttpServletRequest request, HttpServletResponse response)                               throws ServletException, IOException {
            try {
                        // Do whatever you want to do
                        } catch (Exception exception) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(exception.getMessage());
            response.flushBuffer();                               
            } finally {
// Clean up ...
            }
}
}

In the client side, you can get the server error message (after checking that the readyState of the XML HTTP Request (XHR) is 4, and the status of the XHR object is 500) from the responseText of the XHR object.
If you are using Dojo framework then in the Dojo xhrPost API, in order to get the error message, you can get it from the ioArgs parameter of the error callback as follows:
dojo.xhrPost( {
            url: 'service URL',
            content: {},
            handleAs: 'text',
            load: function(response, ioArgs) {
                        // Do something
            },
            error: function(response, ioArgs) {
                        alert("Failed while doing the operation: " + ioArgs.xhr.response);
            }
});

Using the ioArgs.xhr.response, you can get the full error message from the server response, you can also get the status code from the ioArgs.xhr.status.

Saturday, October 13, 2012

How to prevent Caching in AJAX requests


Below steps you can perform to prevent caching in AJAX calls.
              To prevent the response from being cached you can set preventCache flag to true while making dojo request.
            var getContentCallback = function (contentAliases) {
            var contentUrl = servletPath + "/content";
            var xhrArgs = {
                        url:contentUrl,
                        handleAs:"json",
                        preventCache: true,
            content:{
                contentType:"Content",
                contentAliases:dojo.toJson(contentAliases)
            }
        };
        var deferred = dojo.xhrGet(xhrArgs);
        return deferred;
    }

When the preventCache will be set to true it will append dojo.preventCache query parameter to the url before making request so in that case it will take the <portlet:resourceURL/> and append the dojo.preventCache query parameter and the value of dojo.preventCache parameter will change every time the xhr call will be made so server will always return the full response.

Tuesday, October 2, 2012

Creating Dojo 1.7.2 Custom Build


Dojo's build system provides a way to "build" Dojo and your other JavaScript resources and CSS files, so they can be more efficiently used in a production environment by your applications.
Many UI and dojo and UI developers face so many issues during creating the dojo custom build. 

Prerequisites:
You need the following installed on your computer to run Dojo’s build system:
 Java 1.4.2 or later (Java 1.5 recommended).
A source build of Dojo, which you can obtain at http://download.dojotoolkit.org/

Below are the steps to create the dojo 1.7.2 custom build.
1)      Create “dojo-customBuild” folder in <dojo src folder>\util\buildscripts\profiles
2)      Create a new profile name as “myprofile.profile.js” and put this file  under “dojo-customBuild” folder which we have created in step 1.
3)      Profile name is “myprofile.profile.js”. Put below entries in this file.
var profile = {
               
                basePath:"../../..",
               
                releaseDir:"./dojoRelease",

                layerOptimize: "shrinksafe.keepLines",
              
                hasReport: true,

                packages:[
                                {name: "dojo",location: "./dojo"},
                                {name: "dijit",location: "./dijit"},
                                {name: "dojox",location: "./dojox"}       
                ],
               
                layers: {
                                'dojo/dojo': {
                                                include: [ "dojo/dojo", "dojo/main", "dojo/i18n", "dojo/selector/acme", "dojo/fx/Toggler", "dijit/form/DropDownButton", "dijit/form/ComboButton", "dijit/PopupMenuItem", "dijit/MenuSeparator", "dijit/TooltipDialog", "dojo/dnd/AutoSource", "dojo/dnd/Target"],
      }
}
};

4)      Create “mydojo.js”  file and store this file inside in <dojo src folder>\dojo folder .  This is a layer file for dojo custom build as per project structure.
define([
                "dojo/parser",
                "dijit/dijit",
                "dojo/data/ItemFileReadStore",
                "dojo/data/ItemFileWriteStore",
                "dijit/layout/ContentPane",
                "dijit/layout/BorderContainer",
                "dijit/layout/TabContainer",
                "dijit/layout/StackContainer",
                "dijit/Tree",
                 "dijit/layout/AccordionPane",
                 "dijit/layout/AccordionContainer",
                 "dijit/Dialog",
                 "dijit/tree/ForestStoreModel",
                 "dijit/form/Button",
                 "dijit/form/ToggleButton",
                 "dojox/grid/TreeGrid",
                 "dojox/grid/DataGrid"],function(parser,dijit,ItemFileReadStore,ItemFileWriteStore,ContentPane,BorderContainer,TabContainer,StackContainer,Tree,AccordionPane,AccordionContainer,Dialog,ForestStoreModel,Button,ToggleButton,TreeGrid,DataGrid){
 });
5)      Run build.bat --profile ./myprofile –release command from  <dojo src folder>\util\buildscripts location.

6)      Custom build would be created inside <dojo src folder>\ dojoRelease

7)      The dojoRelease folder will contain dojo 1.7.2 custom build.