Wednesday, September 4, 2013

Dojo App - loading a template from a url.



One of the features that I would love to see added to the dojo app project is the ability to support restful templates. Currently, it only loads templates from inside a static directory versus fetching a template from a restful endpoint.

So, If you want to be able to add this functionality, below is an example of how to do it. It currently works with the 1.9 dojo version, and is an extension of the current functionality, so all current features will continue to work.

The idea is that if a template is prefixed with the dollar sign - such as:  template: "$/foo/view",  then it will use
dojo/request to do a get on that end point for the returned content and load it into the view. Otherwise, it will  process it normally.

A normal config.json file would look something like:

  "views": {
        "header": {
            "constraint": "top",
            "controller": "foo/controllers/header",
            "template": "foo/views/header.html",  
            "dependencies": [
                "dijit/layout/BorderContainer",
                "dijit/layout/ContentPane",
                "dojox/app/widgets/Container",
                "ontrac/widgets/Navigation"
            ]
        },
        "dashboard": {
            "constraint": "middle",
            "controller": "foo/controllers/dashboard",
            "template": "$/foo/view/dashboard",        
            "dependencies": [
                "dijit/layout/BorderContainer",
                "dijit/layout/ContentPane",
                "dojox/app/widgets/Container"
            ]
        }

Below is the Gist of the code - Hope it helps, as it wasn't difficult to add the functionality. I don't make guarantees on cross domain requests.. I've not tested that, so if you need that functionality, you'll need to handle the issues/security that is involved in that.

Make sure you include the following dependencies in the AMD loader when overloading:

"dojox/app/View", "dojo/Deferred", "dojo/when", "dojo/_base/lang", "dojo/request"


No comments:

Post a Comment