knockout js:ko.utils.arrayFilter无法正常工作

时间:2016-09-07 02:39:19

标签: javascript knockout.js

所以我最近使用knockout.js将我希望成为过滤系统的内容插入到我的项目中。我将以下代码插入到我的项目中。

self.pointsFilter = ko.computed(function(){
    return ko.utils.arrayFilter(self.pointsList(), function(pointItem){
      return pointItem.done = true;
    })
  });

以前是我的现有代码:

var point = function(obj){
  var self = this;

  this.name = obj.name;
  this.street = obj.street;
  this.city = obj.city;
  this.state = obj.stat;
  this.zip = obj.zip;
  this.food = ko.observable(obj.food);
  this.lat = ko.observable(obj.lat);
  this.lng = ko.observable(obj.lng);
  this.map = map;

  this.fullAddress = function(){
    return self.street + self.city + "</br>" + self.state + self.zip;
  };

  this.formattedAddress = function(){
    var currentStreet = self.street + self.city + self.state + self.zip;
    var newAddress = currentStreet.replace(/ /g, '+');
    return newAddress;
  };

  this.formattedName = function(){
    var newName = self.name.replace(/ /g, '');
    return newName;
  }

}

var viewModel = function(){
  var self = this;
  var marker;

  this.pointsList = ko.observableArray([]);

  points.forEach(function(pointItem){
    self.pointsList.push(new point(pointItem));
  });

  self.pointsList().forEach(function(pointItem){

    console.log(pointItem.formattedName());
    console.log(pointItem.formattedAddress());

    pointItem.marker = marker;

    marker = new google.maps.Marker({
      position: new google.maps.LatLng(pointItem.lat(), pointItem.lng()),
      map: map,
      animation: google.maps.Animation.DROP,
    });

    google.maps.event.addListener(marker, 'click', function(){
      if(infowindow.marker != marker){
        marker.setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png'),
        infowindow.open(map, this);
        infowindow.setContent('<div><h1>' + pointItem.name +
        '</h1><h2>' + pointItem.fullAddress() +
        '</h2><img src=' + streetViewRequest +
        '><div id="instafeed">' + '</div</div>' + instafeedRequest());
      }
    });

  });    

  self.pointsFilter = ko.computed(function(){
    return ko.utils.arrayFilter(self.pointsList(), function(pointItem){
      return pointItem.done = true;
    })
  })

}

如果您想要填充github存储库,可以在此处找到它 current github repository

1 个答案:

答案 0 :(得分:1)

在过滤器的Boolean语句中需要return结果才能返回已完成值为true的内容。你的单身等于做作业而不是Boolean比较。

换句话说,你需要一个double(或三个等于非强制值),如下所示:

return pointItem.done == true;