传单JS面向对象的最佳方法
LeafletJS best approach to object oriented
我正在用传单js做一个小项目,我想知道什么是实现OOP的最佳方法。好的,交易是这样的:
创建一个使用标记的"类",这些标记来自数据库。所以,我想我会创建一个对象数组,我说得对吗?
我的第一个问题是,我必须创建一个自定义类,将标记对象作为该类的属性,还是从传单中扩展L.marker类?
第二个问题:如何在每个标记上绑定点击事件,这样这个事件就可以调用和ajax请求,并绑定一个带有信息的弹出窗口。
到目前为止,这是我的代码:
<script>
var map = L.map('map').setView([25.55246, -103.50328], 18);
var marcas = [];
L.tileLayer('https://{s}.tiles.mapbox.com/v3/kinopio.kikc39gj/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
function Marcador(marca, usuario, seccional, identificador){
this.marca = marca;
this.identificador = identificador;
this.usuario = usuario;
this.seccional = seccional;
}
Marcador.prototype.verUsuario = function(){
console.log(this.usuario);
}
Marcador.prototype.verVotos = function(){
$.ajax({
type: 'GET',
url: '/votos/' + this.identificador,
datatype: 'json',
success:function(data){
msg = $.parseJSON(data);
//marcador.bindPopup('Votos: ' + msg[0].Votos ).openPopup();
console.log(msg[0].Votos);
}
});
}
function onMapClick(e) {
//When clicking in the map, create a marker so i can register a new one to the DB...
var formulario = "<form action='/registro' id='registro' method='post'> " +
"<input name='usuario' placeholder='usuario'> " +
"<input name='password' placeholder='passwword'>" +
"<input name='seccional' placeholder='seccional'>" +
"<input name='latitud' type='hidden' value = " + e.latlng.lat + ">" +
"<input name='longitud' type='hidden' value = " + e.latlng.lng + ">" +
"<input type='submit' value='Aceptar'>" +
"</form>"
var marker = L.marker( e.latlng ).addTo( map ).bindPopup( formulario );
marker.on("popupopen", function(){
var forma = $('#registro');
forma.on('submit', function(ev){
ev.preventDefault();
$.ajax({
type: forma.attr('method'),
url: forma.attr('action'),
data: forma.serialize(),
success:function(data){
marker.closePopup();
marker.bindPopup( 'Usuario registrado exitosament' ).openPopup();
}
});
});
});
}
function cargarRegistros(e){
e.preventDefault();
$.ajax({
type: 'GET',
url: '/registrados',
datatype: 'json',
success: function(d){
$.each(d.marcadores, function(i, item){
marca = new L.marker( [ parseFloat(item.latitud), parseFloat( item.longitud) ] )
.addTo(map).bindPopup();
marcas.push( new Marcador( marca, item.usuario, item.seccional, item.identificador ) );
});
}
});
return marcas;
}
map.on('click', onMapClick);
function clickMarcador(e){
console.log(e.target + '---' + e.currentTarget);
if (e.target.id !== e.currentTarget) {
console.log('hola');
}
e.stopPropagation();
}
$('#registrados').on('click', cargarRegistros);
</script>
我只想扩展L.Marker
类以包括所需的功能,例如,点击事件,当标记的onAdd
方法被触发时,你可以挂起它,并在onRemove
方法上取消挂起它:下面是一个代码示例(使用来自jQuery的$.getJSON):
L.CustomMarker = L.Marker.extend({
onAdd: function (map) {
this.on('click', this.clickHandler);
L.Marker.prototype.onAdd.call(this, map);
},
onRemove: function (map) {
this.off('click', this.clickHandler);
L.Marker.prototype.onRemove.call(this, map);
},
clickHandler: function (e) {
$.getJSON('data.json', function (data) {
e.target.bindPopup(data.myProperty);
e.target.openPopup();
}, this);
}
});
L.customMarker = function (latLng, options) {
return new L.CustomMarker(latLng, options);
}
以下是Plunker的一个工作示例:http://plnkr.co/edit/hNlvhC?p=preview
当然,这是非常粗糙的,在实际实现中,如果弹出窗口已经绑定,则会检查click
事件,这样您就不会绑定它两次,但它可以让您很好地了解扩展L.Marker
的可能性
关于存储标记,您可以只使用L.LayerGroup
和对地图的那个。创建标记时,您需要将其添加到组中。这样,您就可以使用层组的实用程序方法,如hasLayer
、getLayer
、clearLayers
、eachLayer
等。
以下是L.LayerGroup
的参考:http://leafletjs.com/reference.html#layergroup
相关文章:
- 有条件更新d3.js力图中节点的最佳方法
- 将jQuery.ech()方法转换为本地JavaScript抽象的最佳方法是什么
- 处理浮点错误的最佳方法是什么
- 实现比较方法的最佳实践是什么;s的比较类型是在运行时选择的
- 从数组中删除元素的最佳方法是:javascript/jquery
- 以编程方式填充组合框道场 (1.8) 的最佳方法是什么?
- 列出没有 mysql 的元素的最佳方法是什么
- 在jquery库中重新定义方法的最佳方法
- Javascript,用vars创建对象的最佳方法
- 使用 jQuery 从 HTML 中获取某些值的最佳方法
- 在变量中保存值的最佳方法是在应用程序关闭后使用,然后使用apachecordova在android中重新启动
- 在d3.json中使用d3.csv组合多个csv文件数据输入的最佳方法是什么
- 用jasmine测试JavaScriptUI的最佳方法
- 使用javascript:在没有阻止html标记(<b>、<p>等)的情况下,阻止脚本的最佳方法
- 如果我返回表,检查 Ajax 调用是否为 200 OK的最佳方法是什么
- JavaScript - 创建可链接函数时的最佳方法是什么
- 在上下文菜单项单击上显示侧边栏/弹出窗口的最佳方法是什么
- 在 JavaScript 中获取范围的随机数的最佳方法
- 在对象类上实现 jquery 作用域的最佳方法
- 将同步函数包装到承诺中的最佳方法是什么?