2013-02-02 38 views
0

如何覆盖OpenLayers.Protocol.HTTP的URL?我试图如何覆盖OpenLayers.Protocol.HTTP的URL?

searchformPanel.protocol.url

和它的作品(含执行console.log选中),但最终原始URL发送(在下面的代码:网址:“/ FS /')(见附图)。

output from firebug

这是代码:

var searchformPanel = new GeoExt.form.FormPanel({ 
    border: false, 
    width: 250, 
    protocol: new OpenLayers.Protocol.HTTP({ 
    url: '/fs/', 
    format: new OpenLayers.Format.GeoJSON() 
    }), 
    items:[{ 
    xtype: 'combo', 
    id: 'idcombo', 
    store: new Ext.data.SimpleStore({ 
     fields:['fsclass','ollayer'], 
     data:[["Boreholes","Boreholes"],["All_layers","All layers"]] 
    }), 
    displayField: 'ollayer', 
    valueField: 'fsclass', 
    fieldLabel: 'Layer', 
    emptyText: 'select a layer', 
    submitValue: false, 
    selectOnFocus: true, 
    mode: 'local', 
    typeAhead: true, 
    editable: false, 
    forceSelection: true, 
    triggerAction: 'all' 
    },{ 
    xtype: 'textfield', 
    id: 'idtextfield', 
    fieldLabel: 'Find features', 
    emptyText: 'enter word', 
    name: 'comments__like', 
    allowBlank: false 
    }], 
    listeners:{ 
    actioncomplete: function(form, action){ 
     searchShowTip(action.response.features); 
    } 
    }, 
    buttons:[{ 
    text: 'search', 
    listeners:{ 
     click: function(){ 

     var comboLayer = Ext.getCmp('idcombo').getRawValue(); 
     var keyword = Ext.getCmp('idtextfield').getRawValue(); 

     var newUrl = '/fs/' + comboLayer + '?format=GeoJSON&comments__ilike=' + keyword + '&queryable=comments'; 
     console.log('1:' + newUrl); 

     //this gets '/fs/' from the searchformPanel 
     console.log('2:' + searchformPanel.protocol.url); 

     searchformPanel.protocol.url = newUrl; 
     console.log('3:' + searchformPanel.protocol.url); 

     searchformPanel.search(); 
     } 
    } 
    }] 

});

请对此有任何支持,非常欢迎,谢谢!

回答

0

最后它的工作!!!!!!!!!诀窍是在formPanel之外接受协议,并将newUrl替换为“protocol.options.url = newUrl;”。直到我检查了OpenLayers的HTTP.js的第180行时,我才知道这个“选项”,因为我试图使用protocol.read(),并且收到指向此行的错误(如解释here)。另外,我重新发送了formPanel的“comments”和“queryables”(所以我删除了上面的“keyword”),因为它是我自己发布在这个原始问题中的。下面是一段代码,并获得成功,简洁美观:

buttons: [{ 
    text: 'Search', 
    handler: function() { 
     comboLayer = Ext.getCmp('idcombo').getValue(); 
     newUrl = '/fs/' + comboLayer + '?format=GeoJSON'; 
     protocol.options.url = newUrl; 
     formPanel.search(); 
     } 
    }] 

另外,我感到困惑变量和局部/全局范围如何协同工作,我想我想出了这个解决方案,因为我明白,在“函数”里面写东西(因为上面所有的东西在处理程序中调用的函数()中)使得这些变量在函数被声明之前和之后被另一个变量读取,不确定它是否清楚我之前解释的内容,但当然this link解释得更好。

希望这可以帮助更多的人,特别是像javascript和openlayers这样的新人!

0

您的数据格式不正确:

data: [["Boreholes","Boreholes"],["All_layers","All layers"]] 

应该是这样的:

data: { "Boreholes": "Boreholes", "All_layers": "All layers" } 
+0

组合框不以这种方式工作,它不显示Boreholes All_layers – Gery