在 JSF 组件的 JS 脚本中更新数据模型
Update data model in JS script in JSF component
我正在使用 jsf2leaf 库在页面上显示地图。对于我的 purpose,我将源代码替换为我的项目并更改了组件。
组件:
<cc:interface componentType="mapAdvanced">
<cc:attribute name="map" type="java.lang.Object" required="true"/>
</cc:interface>
<cc:implementation>
<h:outputStylesheet name="system/styles/leaflet.css" target="head"/>
<h:outputScript name="system/scripts/leaflet.js" target="head"/>
<h:outputStylesheet name="system/styles/leaflet.labelOverlay.css" target="head"/>
<h:outputStylesheet name="system/styles/markercluster.css" target="head"/>
<h:outputScript name="system/scripts/markercluster.js" target="head"/>
<div id="#{cc.clientId}" style="width:#{cc.map.width}; height:#{cc.map.height}"/>
<c:set var="mapVar" value="map_#{cc.clientId.replaceAll('-','_')}"/>
<script type="text/javascript">
var #{mapVar};
function refreshMap() {
if (#{mapVar} == null) {
#{mapVar} = L.map('#{cc.clientId}', {
center: [#{cc.map.center.latitude}, #{cc.map.center.longitude}],
dragging: #{cc.map.draggingEnabled},
zoomControl: #{cc.map.zoomControl},
zoom: #{cc.map.zoom}
});
}
for (layer in #{mapVar}.layers) {
#{mapVar}.removeLayer(layer);
}
#{mapVar}.center = [#{cc.map.center.latitude}, #{cc.map.center.longitude}];
#{mapVar}.dragging = #{cc.map.draggingEnabled};
#{mapVar}.zoomControl = #{cc.map.zoomControl};
#{mapVar}.zoom = #{cc.map.zoom};
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
id: 'osm',
attribution: '#{cc.map.attribution}',
maxZoom: #{cc.map.maxZoom},
minZoom: #{cc.map.minZoom}
}).addTo(#{mapVar});
//Layers
<ui:repeat value = "#{cc.map.layers}" var = "layer">
var layer = #{layer.clusterEnabled}? new L.MarkerClusterGroup({
disableClusteringAtZoom: #{layer.clusterDisableAtZoom},
maxClusterRadius: #{layer.clusterMaxRadius}
}) : new L.LayerGroup();
<ui:repeat value = "#{layer.labels}" var = "label">
var options = {
offset: null,
cssClass: 'leaflet-label-overlay'
},
txt = new L.LabelOverlay([#{label.position.latitude}, #{label.position.longitude}],
'#{label.value}', options);
#{mapVar}.addLayer(txt);
</ui:repeat>
</ui:repeat >
layers.addTo(#{mapVar});
setTimeout(function () {
#{mapVar}.invalidateSize();
}, 300);
}
refreshMap();
</script>
</cc:implementation>
还有一页:
<h:form id="view_table">
<p:panel id="mapa_panel" style="width: 500px; height: 500px;">
<sys:mapAdvanced id="mapa" map="#{selectionsViewModel.map}" />
</p:panel>
<p:commandButton value="Update" type="submit" action="#{selectionsViewModel.update()}" update="view_table-mapa"/>
</h:form>
在 selectionsViewModel.update()
中,我更改了数据模型并调用 refreshMap()
来更新地图组件。我检查了浏览器的调试器,放置脚本之间没有区别 - 在 div 或 div 之外。两者都出现 return 在响应 os post 一个正确的 html 与新数据。但是更新后我的组件从页面上消失了。
我理解错了。
首先,脚本确实应该在 div 中,因为外部更新引用了 div id。
其次,执行 js 代码,但 chrome 调试器不知何故不会在断点处停止。我将更新地图更改为:
旧:
if (#{mapVar} == null) {
#{mapVar} = L.map('#{cc.clientId}',
{center: [#{cc.map.center.latitude}, #{cc.map.center.longitude}],
dragging: #{cc.map.draggingEnabled},
zoomControl: #{cc.map.zoomControl},
zoom: #{cc.map.zoom}});
}
for (layer in #{mapVar}.layers) {
#{mapVar}.removeLayer(layer);
}
新:
if (#{mapVar} != null) {
#{mapVar}.remove();
}
现在,一切正常。
我正在使用 jsf2leaf 库在页面上显示地图。对于我的 purpose,我将源代码替换为我的项目并更改了组件。
组件:
<cc:interface componentType="mapAdvanced">
<cc:attribute name="map" type="java.lang.Object" required="true"/>
</cc:interface>
<cc:implementation>
<h:outputStylesheet name="system/styles/leaflet.css" target="head"/>
<h:outputScript name="system/scripts/leaflet.js" target="head"/>
<h:outputStylesheet name="system/styles/leaflet.labelOverlay.css" target="head"/>
<h:outputStylesheet name="system/styles/markercluster.css" target="head"/>
<h:outputScript name="system/scripts/markercluster.js" target="head"/>
<div id="#{cc.clientId}" style="width:#{cc.map.width}; height:#{cc.map.height}"/>
<c:set var="mapVar" value="map_#{cc.clientId.replaceAll('-','_')}"/>
<script type="text/javascript">
var #{mapVar};
function refreshMap() {
if (#{mapVar} == null) {
#{mapVar} = L.map('#{cc.clientId}', {
center: [#{cc.map.center.latitude}, #{cc.map.center.longitude}],
dragging: #{cc.map.draggingEnabled},
zoomControl: #{cc.map.zoomControl},
zoom: #{cc.map.zoom}
});
}
for (layer in #{mapVar}.layers) {
#{mapVar}.removeLayer(layer);
}
#{mapVar}.center = [#{cc.map.center.latitude}, #{cc.map.center.longitude}];
#{mapVar}.dragging = #{cc.map.draggingEnabled};
#{mapVar}.zoomControl = #{cc.map.zoomControl};
#{mapVar}.zoom = #{cc.map.zoom};
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
id: 'osm',
attribution: '#{cc.map.attribution}',
maxZoom: #{cc.map.maxZoom},
minZoom: #{cc.map.minZoom}
}).addTo(#{mapVar});
//Layers
<ui:repeat value = "#{cc.map.layers}" var = "layer">
var layer = #{layer.clusterEnabled}? new L.MarkerClusterGroup({
disableClusteringAtZoom: #{layer.clusterDisableAtZoom},
maxClusterRadius: #{layer.clusterMaxRadius}
}) : new L.LayerGroup();
<ui:repeat value = "#{layer.labels}" var = "label">
var options = {
offset: null,
cssClass: 'leaflet-label-overlay'
},
txt = new L.LabelOverlay([#{label.position.latitude}, #{label.position.longitude}],
'#{label.value}', options);
#{mapVar}.addLayer(txt);
</ui:repeat>
</ui:repeat >
layers.addTo(#{mapVar});
setTimeout(function () {
#{mapVar}.invalidateSize();
}, 300);
}
refreshMap();
</script>
</cc:implementation>
还有一页:
<h:form id="view_table">
<p:panel id="mapa_panel" style="width: 500px; height: 500px;">
<sys:mapAdvanced id="mapa" map="#{selectionsViewModel.map}" />
</p:panel>
<p:commandButton value="Update" type="submit" action="#{selectionsViewModel.update()}" update="view_table-mapa"/>
</h:form>
在 selectionsViewModel.update()
中,我更改了数据模型并调用 refreshMap()
来更新地图组件。我检查了浏览器的调试器,放置脚本之间没有区别 - 在 div 或 div 之外。两者都出现 return 在响应 os post 一个正确的 html 与新数据。但是更新后我的组件从页面上消失了。
我理解错了。 首先,脚本确实应该在 div 中,因为外部更新引用了 div id。 其次,执行 js 代码,但 chrome 调试器不知何故不会在断点处停止。我将更新地图更改为:
旧:
if (#{mapVar} == null) {
#{mapVar} = L.map('#{cc.clientId}',
{center: [#{cc.map.center.latitude}, #{cc.map.center.longitude}],
dragging: #{cc.map.draggingEnabled},
zoomControl: #{cc.map.zoomControl},
zoom: #{cc.map.zoom}});
}
for (layer in #{mapVar}.layers) {
#{mapVar}.removeLayer(layer);
}
新:
if (#{mapVar} != null) {
#{mapVar}.remove();
}
现在,一切正常。