2016-09-07 69 views
0

所以我最近插入了我想成爲一個過濾系統到我的項目使用knockout.js。我將下面的代碼插入到我的項目中。淘汰賽js:ko.utils.arrayFilter工作不正常

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

這裏是我exisiting代碼前面:

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

+0

https://stackoverflow.com/questions/15647114/if-statement的可能欺騙 - 被忽視 – JohnnyHK

回答

1

你需要Boolean結果在你的return過濾器的聲明僅返回已完成值爲true的事情。您的一個等於做了一項任務,而不是一個Boolean比較。

換句話說,你需要一個雙(非威嚇值或三重等於),像這樣:

return pointItem.done == true; 
+0

所以我有一個過濾器的js和我有過濾器框,但它不會做任何事情。所以我想獲得他們輸入的名稱以啓用該位置的infowindow。我是否需要添加新的事件列表或以某種方式觸發已存在的信息窗口... –