Changeset 1950 for trunk


Ignore:
Timestamp:
2011-09-14 12:14:34 (8 months ago)
Author:
mpo
Message:

Allow checking for required dependencies before doing remote options-refreshes.
This avoids needless error-calls to backend-uris that don't exist.
It also fixes #278

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/modules/kauri-forms/kauri-forms-framework/src/main/kauri/static-{build}.key/kauri.forms/control.js

    r1948 r1950  
    12031203         
    12041204        if (opts) { 
    1205             var options = kf.Options.create(opts, this.getType()); 
     1205            var options = kf.Options.create(opts, this.getType(), this); 
    12061206            if (options.toShare()) {  
    12071207                options = this.getType().share("options", options); 
  • trunk/modules/kauri-forms/kauri-forms-framework/src/main/kauri/static-{build}.key/kauri.forms/field.js

    r1948 r1950  
    4646     * @static 
    4747     */ 
    48     Options.create = function(config, type) { 
     48    Options.create = function(config, type, control) { 
    4949        var opts = new Options(); 
    5050        $.extend(opts, config); 
     
    6363        } 
    6464         
     65        //translate opt.requires into an array. 
     66        if (opts.requires === undefined || opts.requires === null || opts.requires === false) { 
     67            opts.requires = []; 
     68        } else if (opts.requires === true || opts.requires === "all") { 
     69            opts.requires = []; 
     70            if (control && control.depends) { 
     71                var depname; 
     72                for(depname in control.depends) { 
     73                    opts.requires.push(depname); 
     74                } 
     75            } 
     76        }  
    6577        return opts; 
    6678    } 
     
    222234    Options.prototype.refresh = function() { 
    223235 
    224         if (this.refreshable()) { 
    225             this.uriContext = this.uriContext || {}; 
    226             var uri = this.uri.expand(this.uriContext); 
    227             var me = this; 
    228             var onSuccess = function( data, status, xhr) { 
    229                 if (xhr && me.lastXhr === xhr) { //ignore responses that are not linked to the last request  
    230                     me.parseOptions(data); 
    231                     me.lastXhr = null; 
    232                 } 
    233             }; 
    234             var onError = function( xhr, status, err) { 
    235                 if (xhr && me.lastXhr === xhr) {  
    236                     me.clearOptions(); 
    237                     me.lastXhr = null; 
    238                 } 
    239             }; 
    240  
    241             // assign lastXhr to make sure only the return of this last one is accepted  
    242             $.ajax( { 
    243                 // take async setting from setup 
    244                 type       : "GET", 
    245                 cache      : false, 
    246                 url        : uri, 
    247                 processData: false, 
    248                 dataType   : "json", 
    249                 success    : onSuccess, 
    250                 error      : onError, 
    251                 beforeSend : function(xhr) { 
    252                     // setting datatype to json should be enough, unfortunately 
    253                     // the restlet content negotiation chokes on the resulting 
    254                     // accept header: application/json, text/javascript, */* 
    255                     // and doesn't prefer the json variant. 
    256                     // see issue #95 
    257                     xhr.setRequestHeader("Accept", "application/json"); 
    258                     me.lastXhr = xhr; 
    259                 } 
    260             }); 
    261         } 
    262     }; 
    263  
     236        if (! this.refreshable() ){ 
     237            return; 
     238        } 
     239         
     240        if (! this.uriContextComplete() ) { 
     241            return; 
     242        } 
     243         
     244        var uri = this.uri.expand(this.uriContext); 
     245        var me = this; 
     246        var onSuccess = function( data, status, xhr) { 
     247            if (xhr && me.lastXhr === xhr) { //ignore responses that are not linked to the last request  
     248                me.parseOptions(data); 
     249                me.lastXhr = null; 
     250            } 
     251        }; 
     252        var onError = function( xhr, status, err) { 
     253            if (xhr && me.lastXhr === xhr) {  
     254                me.clearOptions(); 
     255                me.lastXhr = null; 
     256            } 
     257        }; 
     258 
     259        // assign lastXhr to make sure only the return of this last one is accepted  
     260        $.ajax( { 
     261            // take async setting from setup 
     262            type       : "GET", 
     263            cache      : false, 
     264            url        : uri, 
     265            processData: false, 
     266            dataType   : "json", 
     267            success    : onSuccess, 
     268            error      : onError, 
     269            beforeSend : function(xhr) { 
     270                // setting datatype to json should be enough, unfortunately 
     271                // the restlet content negotiation chokes on the resulting 
     272                // accept header: application/json, text/javascript, */* 
     273                // and doesn't prefer the json variant. 
     274                // see issue #95 
     275                xhr.setRequestHeader("Accept", "application/json"); 
     276                me.lastXhr = xhr; 
     277            } 
     278        }); 
     279    }; 
     280 
     281    /** 
     282     * Checks if all required members in the uri-context are present. 
     283     */ 
     284    Options.prototype.uriContextComplete = function() { 
     285        this.uriContext = this.uriContext || {}; 
     286 
     287        var i = 0, last = this.requires.length; 
     288        for (i = 0; i<last ; i++) { 
     289            var requiredContext = this.uriContext[this.requires[i]]  
     290            if (requiredContext === undefined || requiredContext === null) { 
     291                return false; 
     292            } 
     293        } 
     294         
     295        return true; 
     296    } 
    264297 
    265298    /** 
  • trunk/samples/kauri-forms-sample/src/main/kauri/pages/selection-control.html.xml

    r1890 r1950  
    9090                      "depends": {"carMake": "/carMake"}, 
    9191                      "options": { 
     92                          "requires": "all", 
    9293                          "uri": "${publicUri('service:/data/carmake/{carMake}/models')}", 
    9394                          "valueTemplate": "{name}", 
     
    151152              } 
    152153          } 
    153       } 
     154      }; 
    154155 
    155156       
Note: See TracChangeset for help on using the changeset viewer.