summaryrefslogtreecommitdiff
path: root/js/dojo-1.7.2/dojox/app/bind.js
blob: 0d6dedea140744d44684e44efae8f79dfa2f2ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//>>built
define("dojox/app/bind", ["dojo/_base/kernel", "dojo/query" , "dojo/_base/array", "dijit", "dojo/_base/json"], function(dojo, query, array, dijit, djson){
    return function(/*Array of widgets*/widgets, /*Object*/ models){
        array.forEach(widgets, function(item){
            //TODO need to find a better way to get all bindable widgets
            var bindWidgets = query("div[dojoType^=\"dojox.mvc\"],div[data-dojo-type^=\"dojox.mvc\"]", item.domNode);
            //set ref for each dojox.mvc widgets.
            array.forEach(bindWidgets, function(widget){
                //TODO need to find a better way to know which model the widget is bound to
                //currently, the ref attribute in dojox.mvc.Group cannot be empty, leave
                //explicit string with single quote in ref attribute.
                var ref = widget.getAttribute("ref");
                
                if(ref === null){
                    var refProps = widget.getAttribute("data-dojo-props");
                    if(refProps){
                        try{
                            refProps = djson.fromJson("{" + refProps + "}");
                        }catch(e){
                            // give the user a pointer to their invalid parameters. FIXME: can we kill this in production?
                            throw new Error(e.toString() + " in data-dojo-props='" + extra + "'");
                        }
                        ref = refProps.ref.replace(/^\s*rel\s*:\s*/, "");
                    }
                }
                
                if (ref) {
                    if(ref[0] === "'"){
                        ref = ref.substring(1, ref.length-1);
                    }
                    var model = dojo.getObject(ref, false, models);
                    if (model){
                        dijit.byNode(widget).set("ref", model);
                    }                    
                }
            }, this);
        }, this);
        
    }
});