ValueAccessor在自定义绑定中为空(knockoutjs& googlemap)

时间:2016-05-19 06:54:58

标签: javascript google-maps knockout.js

我有一个通过ajax请求获取地点的视图模型。这些地方即将到来,但当我尝试在自定义绑定谷歌地图中使用它时,valueAccessor为空。 你能解释一下我在做什么错吗?

var  MapViewModel  = function(){

	var self = this;
    var yelp = new YelpDataProvider();
    var categories = [{"id": 1,"name" : "Bars"}, {"id": 2,"name" : "Gyms"}];
    var addresses = [];
	self.places = ko.observableArray();
	self.selectCategory  = ko.observable();
	
    self.selectCategory.subscribe(function(category) {
    	var _filtered = addresses;

    	if(typeof category != "undefined")
    		_filtered = _.where(_filtered, {'categoryId': category});
    	
        yelp.getDataForPlaces(_filtered).then(function(place){
			self.places(place);
		});
    });

	yelp.getDataForPlaces(addresses).then(function(place){
		self.places(place);
	})

}


ko.bindingHandlers.googlemap = {
    init: function (element, valueAccessor) {
    	debugger;
        var value = ko.utils.unwrapObservable(valueAccessor());
        console.log(value);
        console.log(valueAccessor());
        var centerPoint = new google.maps.LatLng(value[0].location.coordinate.latitude,
        				 value[0].location.coordinate.longitude);

  		var myWrapper = $("#wrapper");
  		$("#menu-toggle").click(function(e) {
    		e.preventDefault();
    		$("#wrapper").toggleClass("toggled");
    		myWrapper.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(e) {
      			// code to execute after transition ends
      			google.maps.event.trigger(map, 'resize');
    		});
  		});

 		 // create the map
		  var myOptions = {
		    zoom: 5,
		    center: centerPoint,
		    mapTypeControl: true,
		    mapTypeControlOptions: {
		      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
		    },
		    navigationControl: true,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		 }

	  	var map = new google.maps.Map(element, mapOptions)

	  	var infowindow = new google.maps.InfoWindow({
		  size: new google.maps.Size(150, 50)
		});

		google.maps.event.addListener(map, 'click', function() {
			infowindow.close();
		});


		_.each(value,function(place){
			var _point = new google.maps.LatLng(place[0].location.coordinate.latitude,
				        				 place[0].location.coordinate.longitude);
				var marker = createMarker(point, place.businesses[0].id,
				 businesses[0].snippet_text)
		});

		google.maps.event.addListener(map, 'click', function() {
    			infowindow.close();
  		  });

		google.maps.event.addDomListener(window, 'load', initialize);
		  
    }
};

ko.applyBindings(MapViewModel);
<div id="map_canvas" data-bind="googlemap: places"></div>

1 个答案:

答案 0 :(得分:0)

我不确定为什么你不能访问“地方”,但下面是另一种你可以尝试在绑定处理程序中访问它的方法 -

  1. 更新html中的绑定以传递viewModel -

    <div id="map_canvas" data-bind="googlemap: places, viewModel: $root"></div>
    
  2. 将“allBindingAccessors”作为参数包含在处理程序中 -

    ko.bindingHandlers.googlemap = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            var viewModel = allBindingsAccessor().viewModel;
            var value = viewModel.places();
            // Do more stuff
        }
    }
    
  3. 希望这有效:P