Ticket #6723: connect.patch

File connect.patch, 3.3 kB (added by ttrenka, 3 months ago)
  • _base/connect.js

     
    100100        //              If obj[event] is null, it is assigned a function. 
    101101        // 
    102102        //              The return value is a handle that is needed to  
    103         //              remove this connection with dojo.disconnect. 
     103        //              remove this connection with dojo.disconnect.  The handle 
     104        //              also contains methods for suspending and resuming the  
     105        //              connection. 
    104106        // 
    105107        // obj:  
    106108        //              The source object for the event function.  
     
    144146        //      |       dojo.disconnect(link); 
    145147        // 
    146148        // example: 
     149        //              Using return value to suspend and resume the connection: 
     150        //      |       var link = dojo.connect(obj, "onchange", ui, "update"); 
     151        //      |       ... 
     152        //      |       link.suspend(); 
     153        //      |       ... 
     154        //      |       link.resume(); 
     155        //      |       ... 
     156        //      |       dojo.disconnect(link); 
     157        // 
     158        // example: 
    147159        //              When onglobalevent executes, watcher.handler is invoked: 
    148160        //      |       dojo.connect(null, "onglobalevent", watcher, "handler"); 
    149161        // 
     
    174186        // absorb any additional arguments 
    175187        for(var l=a.length; i<l; i++){  args.push(a[i]); } 
    176188        // do the actual work 
    177         return dojo._connect.apply(this, args); /*Handle*/ 
     189        return dojo.mixin(dojo._connect.apply(this, args), dojo._connectFns, { _args: args }); /*Handle*/ 
    178190} 
    179191 
     192dojo._connectFns = { 
     193        suspend: function() { 
     194                dojo._disconnect.apply(dojo, this); 
     195        }, 
     196        resume: function() { 
     197                dojo.mixin(this, dojo._connect.apply(dojo, this._args)); 
     198        } 
     199} 
     200 
    180201// used by non-browser hostenvs. always overriden by event.js 
    181202dojo._connect = function(obj, event, context, method){ 
    182203        var l=dojo._listener, h=l.add(obj, event, dojo.hitch(context, method));  
  • tests/_base/connect.js

     
    133133                        obj.baz(); 
    134134                        t.is('baz', out); 
    135135                }, 
     136                function suspendResumeTest(t){ 
     137                        var out = ''; 
     138                        var obj = { 
     139                                foo: function() { 
     140                                        out += 'foo'; 
     141                                }, 
     142                                bar: function() { 
     143                                        out += 'bar'; 
     144                                }, 
     145                                baz: function() { 
     146                                        out += 'baz'; 
     147                                } 
     148                        }; 
     149                        //      initial test. 
     150                        var foobar = dojo.connect(obj, "foo", obj, "bar"); 
     151                        dojo.connect(obj, "bar", obj, "baz"); 
     152                        // 
     153                        out = ''; 
     154                        obj.foo(); 
     155                        t.is('foobarbaz', out); 
     156                        // 
     157                        out = ''; 
     158                        obj.bar(); 
     159                        t.is('barbaz', out); 
     160                        // 
     161                        out = ''; 
     162                        obj.baz(); 
     163                        t.is('baz', out); 
     164                         
     165                        // connect foo and baz, suspend foobar 
     166                        var foobaz = dojo.connect(obj, "foo", obj, "baz"); 
     167                        foobar.suspend(); 
     168                        // 
     169                        out = ''; 
     170                        obj.foo(); 
     171                        t.is('foobaz', out); 
     172                        // 
     173                        out = ''; 
     174                        obj.bar(); 
     175                        t.is('barbaz', out); 
     176                        // 
     177                        out = ''; 
     178                        obj.baz(); 
     179                        t.is('baz', out); 
     180 
     181                        //      suspend foobaz, resume foobar 
     182                        foobaz.suspend(); 
     183                        foobar.resume(); 
     184                        out = ''; 
     185                        obj.foo(); 
     186                        t.is('foobarbaz', out); 
     187                        // 
     188                        out = ''; 
     189                        obj.bar(); 
     190                        t.is('barbaz', out); 
     191                        // 
     192                        out = ''; 
     193                        obj.baz(); 
     194                        t.is('baz', out); 
     195 
     196                        //      resume foobaz, disconnect foobar 
     197                        foobaz.resume(); 
     198                        dojo.disconnect(foobar); 
     199                         
     200                        out=''; 
     201                        obj.foo(); 
     202                        t.is('foobaz', out); 
     203 
     204                        out=''; 
     205                        obj.bar(); 
     206                        t.is('barbaz', out); 
     207 
     208                        out=''; 
     209                        obj.baz(); 
     210                        t.is('baz', out); 
     211                }, 
    136212                function hubConnectDisconnect1000(t){ 
    137213                        t.is(0, markAndSweepTest(1000)); 
    138214                },