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