bind.js 1.9 KB

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