2014-02-12 21 views
1

knockout.js 개념 증명을위한 라이브 검색 기능을 연구 중입니다. ko.computed 배열은 내가 믿어야 할 때 아무 것도 반환하지 않습니다. 어떤 도움이라도 대단히 감사하겠습니다. jsFiddleKnockout.JS 관측 가능한 라이브 검색

self = this; 
self.query = ko.observable('benefits'); 
self.samplesharepoint = [ 
    { title: "benefits", url: "www.benefits.com", sites: "branchburg, branford", topics: "benefits", tagged: "false" }, 
    { title: "health", url: "www.health.com", sites: "indy, laval", topics: "health", tagged: "false"}, 
    { title: "benefits", url: "www.benefits.com", sites: "ponce, genentech", topics: "benefits", tagged: "false"} 
    ]; 
debugger; 
self.pageLinks = ko.observableArray(self.samplesharepoint); 
self.computedPageLinks = ko.computed(function() { 
    return ko.utils.arrayFilter(self.pageLinks, function(item) { 
     return item.title.toLowerCase().indexOf(self.query.toLowerCase()) >= 0; 
    }); 
}); 

답변

3

당신은 일반적인 녹아웃 실수가 : 여기 jsfiddle입니다 self.pageLinksself.query이 관찰 가능한 때문에 - 기능입니다 - 당신은 그 값을 얻기 위해 인수없이 호출 할 필요가있다.

그래서 변경하려면로 계산 :

self.computedPageLinks = ko.computed(function() { 
    return ko.utils.arrayFilter(self.pageLinks(), function(item) { 
     return item.title.toLowerCase().indexOf(self.query().toLowerCase()) >= 0; 
    }); 
}); 

참고 ()

self.pageLinks()self.query() 데모 JSFiddle.

documentation에서 관측 가능 및 계산에 대해 자세히 읽을 수 있습니다.

+0

감사합니다. 그것은 빠르고 감사했습니다. 조용히 실패하는 이유가 있습니까? 어디서나 오류를 찾을 수 없습니다. – rhythmedium

+1

'ko.observableArray'는 공식 인자가없는 함수이고 모든 함수가 공식 인수의 수인'length' 속성을 가지고 있기 때문에 자동으로 실패합니다 -'ko.utils.arrayFilter'는' self.pageLinks'는'length = 0'을 가진 배열이므로 반복을 시작하지 않습니다 ... – nemesv

관련 문제