简体   繁体   English

进行数据绑定:单击的方式类似于marker.addListener()

[英]Making a data-bind:click work like a marker.addListener()

I'm making an app that uses knockoutjs to load google maps and multiple markers from an observableArray. 我正在制作一个使用基因敲除js从observableArray加载Google地图和多个标记的应用程序。 There are corresponding <li> elements for each marker also pulled from my observableArray. 每个标记都有对应的<li>元素,这些元素也从我的observableArray中提取。 Each marker has a marker.addListener('click', function) that pulls up an infowindow which will eventually have some info about that location in pulled from an outside api. 每个标记都有一个marker.addListener('click', function) ,它会拉出一个信息窗口,该窗口最终会从外部api中获得有关该位置的一些信息。 My issue is connecting the <li> elements with its corresponding marker. 我的问题是将<li>元素与其相应的标记连接起来。 I would like to use a data-bind='click:' on a <li> element and have the same result as if I had clicked on the marker. 我想在<li>元素上使用data-bind='click:' ,并获得与单击标记相同的结果。

http://codepen.io/ntibbs/pen/ZbPPBm http://codepen.io/ntibbs/pen/ZbPPBm

I've tried doing something like 我尝试做类似的事情

      this.marker.addListener('click', myFunction);

      this.zoom = function(){
        myFunction;
      }.bind(this);

        function myFunction() {
        if (infowindow) {
          infowindow.close();
        };
        contentString = '<div">'+
          this.title + '</div>';
        infowindow = new google.maps.InfoWindow({
          content: contentString,
          pixelOffset: new google.maps.Size(50, 0),
        });  
        map.setZoom(9);
        map.setCenter(this.getPosition());
        infowindow.open(map, this);
        google.maps.event.addListener(infowindow, 'closeclick', function() {
          map.setZoom(7);
          map.setCenter(mapOptions.center);
        });
      };

But it is not working as I had hoped. 但是它没有按我希望的那样工作。 Any insight on what I need to do differently or whats going wrong with my code would be a huge help. 对我需要做些什么或我的代码出了什么问题的任何见解都将提供巨大的帮助。

The only thing in your viewmodel is the collection of city information. 您的视图模型中唯一的事情就是城市信息的收集。 That's more of a data model than a view model. 与其说视图模型,不如说是数据模型。 In particular, the zoom function for your click binding isn't in your view model. 特别是,视图绑定中没有单击绑定的zoom功能。 It looks like it's part of the binding handler, and you're using this a lot in there, which is confusing to me. 看起来它是绑定处理程序的一部分,并且您在其中经常使用this ,这让我感到困惑。

To get your two types of clicks to do the same thing you really need to have myFunction available to the viewmodel and have the li-span click call it with the same data (or this ) as the marker listeners do. 要使两种类型的单击执行相同的操作,实际上需要使myFunction可用于视图模型,并使li-span单击使用与标记侦听器相同的数据(或this )进行调用。 This probably means making your custom binding handler take an object with several things in it (like callback for clicks). 这可能意味着让您的自定义绑定处理程序接受其中包含多个内容的对象(例如点击的回调)。

You might find this binding for maps helpful. 您可能会发现此绑定对于地图很有帮助。 He gives a demo fiddle . 他给小样提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM