source: Dev/branches/rest-dojo-ui/client/dojox/mvc/Bind.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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