From 07c6d9b4070c93fece429cab96323ffee485bcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2EFernando=20S=C3=A1nchez?= Date: Sat, 13 Oct 2012 16:51:17 +0200 Subject: [PATCH] Added infinite search --- js/app.js | 32 +++++++++++++++++++++++++++++--- player.js | 4 +++- public/index.html | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/js/app.js b/js/app.js index 29283e3..94b82a6 100644 --- a/js/app.js +++ b/js/app.js @@ -228,6 +228,8 @@ $(document).ready(function(){ var self = this; self.results = ko.observableArray([]); + self.hasMoreSearch = ko.observable(true); + self.currentPage = ko.observable(0); self.queryString = ko.observable(); self.shuffle = shuffle; self.current = current; @@ -249,14 +251,38 @@ $(document).ready(function(){ } // Operations - self.getResults = function(form) { - $.getJSON(encodeURI('/search?id="'+self.queryString()+'"'), function(allData) { + self.search = function(page) { + $.getJSON(encodeURI('/search?id='+self.queryString()+'&p='+page), function(allData) { var results = $.map(allData, function(item) { console.log(JSON.stringify(item)); return new Song(item.id,item.group,item.title,item.quality) }); - self.results(results); + console.log("Results:"); + console.log(results); + if(results.length < 10){ + console.log("No more results"); + self.hasMoreSearch(false); + }else{ + self.hasMoreSearch(true); + } + if(results.length > 0){ + for (var i = 0; i < results.length; i += 1) { + self.results.push(results[i]) + } + self.currentPage(page); + } }); + } + + self.getResults = function() { + self.results.removeAll(); + self.search(1); } + self.searchMore = function(){ + if(self.hasMoreSearch()){ + self.search(self.currentPage()+1); + } + } + if(typeof(LocalStorage)!=="undefined"){ alert("Bad luck!"); diff --git a/player.js b/player.js index 5748794..7e76ca2 100644 --- a/player.js +++ b/player.js @@ -25,8 +25,10 @@ app.get('/search', function(req, resp){ var options = { host: 'www.goear.com', port: 80, - path: '/search.php?q='+encodeURIComponent(req.query['id']) + path: '/search/'+encodeURIComponent(req.query['id'].replace(/\s/g , "-"))+'/'+encodeURIComponent(req.query['p']) }; + console.log("Query:"+req.query['id']); + console.log("Querying:"+options.path); var results = []; var html = ''; diff --git a/public/index.html b/public/index.html index 06a0764..c2a7087 100644 --- a/public/index.html +++ b/public/index.html @@ -77,7 +77,7 @@ - +