diff --git a/www/js/models/sql/chapter.js b/www/js/models/sql/chapter.js index da324b28..1c028d56 100755 --- a/www/js/models/sql/chapter.js +++ b/www/js/models/sql/chapter.js @@ -163,8 +163,16 @@ define(function (require) { // results already in collection -- return them options.success(results); deferred.resolve(results); + } else if (options.data.hasOwnProperty('projectid')) { + // find projectid matches + var projectid = options.data.projectid; + results = chapters.filter(function (element) { + return element.attributes.projectid === projectid; + }); + // results already in collection -- return them + options.success(results); + deferred.resolve(results); } else if (options.data.hasOwnProperty('name')) { - // special case -- empty name query ==> reset local copy so we force a retrieve // from the database if (name === "") { diff --git a/www/js/views/SearchViews.js b/www/js/views/SearchViews.js index e770aaf0..65f4c800 100644 --- a/www/js/views/SearchViews.js +++ b/www/js/views/SearchViews.js @@ -1235,6 +1235,7 @@ define(function (require) { onShow: function () { var lstBooks = ""; this.bookList.fetch({reset: true, data: {projectid: this.model.get('projectid')}}); + this.chapterList.fetch({reset: true, data: {projectid: this.model.get('projectid')}}); // initial sort - name this.bookList.comparator = 'name'; this.bookList.sort(); @@ -1254,24 +1255,28 @@ define(function (require) { event.preventDefault(); } var key = $('#search').val(); + if (key.length === 0 && $("#lstBooks").hasClass("hide")) { + // search is cleared out -- show the books UI + this.toggleSearchBrowse(); + return; + } if (key.length > 0 && $("#lstSearch").hasClass("hide")) { // we have something to search for -- show the search UI + this.chapterList.fetch({reset: true, data: {projectid: this.model.get('projectid')}}); this.toggleSearchBrowse(); - // search for the string provided - this.chapterList.fetch({reset: true, data: {name: key}}); - this.chapterList.each(function (model) { + } + // search for the string provided (case-insensitive) + this.chapterList.each(function (model) { + if (model.get("name").toLowerCase().indexOf(key.toLowerCase()) > -1) { lstChapters += chapTemplate(model.attributes); - }); - if (this.chapterList.length > 0) { - $("#lstSearchResults").html(lstChapters); - $("#lblSearchResults").removeAttr("style"); - } else { - $("#lblSearchResults").attr("style", "display:none"); } - } - if (key.length === 0 && $("#lstBooks").hasClass("hide")) { - // search is cleared out -- show the books UI - this.toggleSearchBrowse(); + }); + if (lstChapters.length > 0) { + $("#lstSearchResults").html(lstChapters); + $("#lblSearchResults").removeAttr("style"); + } else { + $("#lblSearchResults").attr("style", "display:none"); + $("#lstSearchResults").html(""); } },