Bind.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. define("dojox/mvc/Bind", [
  2. "dojo/_base/lang",
  3. "dojo/_base/array"
  4. ], function(lang, array){
  5. var mvc = lang.getObject("dojox.mvc", true);
  6. /*=====
  7. mvc = dojox.mvc;
  8. =====*/
  9. return lang.mixin(mvc, {
  10. bind: function(/*dojo.Stateful*/ source, /*String*/ sourceProp,
  11. /*dojo.Stateful*/ target, /*String*/ targetProp,
  12. /*Function?*/ func, /*Boolean?*/ bindOnlyIfUnequal){
  13. // summary:
  14. // Bind the specified property of the target to the specified
  15. // property of the source with the supplied transformation.
  16. // source:
  17. // The source dojo.Stateful object for the bind.
  18. // sourceProp:
  19. // The name of the source's property whose change triggers the bind.
  20. // target:
  21. // The target dojo.Stateful object for the bind whose
  22. // property will be updated with the result of the function.
  23. // targetProp:
  24. // The name of the target's property to be updated with the
  25. // result of the function.
  26. // func:
  27. // The optional calculation to be performed to obtain the target
  28. // property value.
  29. // bindOnlyIfUnequal:
  30. // Whether the bind notification should happen only if the old and
  31. // new values are unequal (optional, defaults to false).
  32. var convertedValue;
  33. return source.watch(sourceProp, function(prop, oldValue, newValue){
  34. convertedValue = lang.isFunction(func) ? func(newValue) : newValue;
  35. if(!bindOnlyIfUnequal || convertedValue != target.get(targetProp)){
  36. target.set(targetProp, convertedValue);
  37. }
  38. });
  39. },
  40. bindInputs: function(/*dojo.Stateful[]*/ sourceBindArray, /*Function*/ func){
  41. // summary:
  42. // Bind the values at the sources specified in the first argument
  43. // array such that a composing function in the second argument is
  44. // called when any of the values changes.
  45. // sourceBindArray:
  46. // The array of dojo.Stateful objects to watch values changes on.
  47. // func:
  48. // The composing function that is called when any of the source
  49. // values changes.
  50. // tags:
  51. // protected
  52. var watchHandles = [];
  53. array.forEach(sourceBindArray, function(h){
  54. watchHandles.push(h.watch("value", func));
  55. });
  56. return watchHandles;
  57. }
  58. });
  59. });