From 633e13b9e8ffd3c5bf0d63afc1dc47316ef82987 Mon Sep 17 00:00:00 2001 From: jakerella Date: Sun, 1 Mar 2015 12:16:40 -0500 Subject: [PATCH] Added 'find' method to model and made viewmodel use that for search; switched searching to do ajax call to json file --- Gruntfile.js | 6 ++++ src/data.json | 44 +++++++++++++++++++++++++++++ src/js/models/beer.js | 39 +++++++++++++++++++++++-- src/js/viewModels/indexViewModel.js | 34 ++-------------------- 4 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 src/data.json diff --git a/Gruntfile.js b/Gruntfile.js index dd5c97e..944ba46 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -36,6 +36,12 @@ module.exports = function( grunt ) { src: [ "assets/*.*" ], dest: "dist/", cwd: "src/" + }, + data: { + expand: true, + src: [ "*.json" ], + dest: "dist/", + cwd: "src/" } }, diff --git a/src/data.json b/src/data.json new file mode 100644 index 0000000..9db0852 --- /dev/null +++ b/src/data.json @@ -0,0 +1,44 @@ +[ + { + "name": "Monkey King", + "brewery": "New Holland", + "description": "Saison", + "abv": 6.6 + }, + { + "name": "Lily Flagg", + "brewery": "Straight to Ale", + "description": "Milk Stout", + "abv": 5 + }, + { + "name": "Black Bavarian", + "brewery": "Sprecher", + "description": "Schwarzbier", + "abv": 6 + }, + { + "name": "Rye Stout", + "brewery": "Flat 12", + "description": "Stout", + "abv": null + }, + { + "name": "Trappist 12", + "brewery": "Westvleteren", + "description": "Trippel", + "abv": 8 + }, + { + "name": "Anniversary IPA", + "brewery": "Yazoo", + "description": "White IPA", + "abv": 5.6 + }, + { + "name": "POTUS 44", + "brewery": "Black Abbey", + "description": "Stout", + "abv": 8.2 + } +] \ No newline at end of file diff --git a/src/js/models/beer.js b/src/js/models/beer.js index 8a26509..e1de46e 100755 --- a/src/js/models/beer.js +++ b/src/js/models/beer.js @@ -1,5 +1,5 @@ window.beerApp = (window.beerApp || {}); -window.beerApp.Beer = (function() { +window.beerApp.Beer = (function($) { "use strict"; var nextId = 0; @@ -28,6 +28,41 @@ window.beerApp.Beer = (function() { } }); + Beer.find = function beerFinder(query, limit, cb) { + // If the calling code didn't provide query or limit, but did give us a callback, use that! + if (typeof query === "function") { cb = query; query = null; } + if (typeof limit === "function") { cb = limit; limit = null; } + + // Some sane defaults... + query = query || {}; + limit = limit || 10; + cb = cb || function(){}; + + if (query && typeof query !== "object") { + // The 'setTimeout' call here keeps the 'find()' method asynchronous + setTimeout(function() { cb(new Error("Sorry, but the query must be an object!")); }, 0); + return; + } + if (limit && typeof limit !== "number") { + setTimeout(function() { cb(new Error("Sorry, but the limit must be a number!")); }, 0); + return; + } + + // Now do our search... + $.ajax({ + url: "/data.json", + data: { query: query, limit: limit }, + dataType: "json", + success: function findResults(results) { + cb(results); + }, + error: function findError(xhr) { + window.console.warn("error finding beer:", xhr); + cb(new Error("Unable to process beer search!")); + } + }); + }; + return Beer; -})(); \ No newline at end of file +})(window.jQuery); \ No newline at end of file diff --git a/src/js/viewModels/indexViewModel.js b/src/js/viewModels/indexViewModel.js index 7d6a7d3..0ea6f7c 100755 --- a/src/js/viewModels/indexViewModel.js +++ b/src/js/viewModels/indexViewModel.js @@ -11,7 +11,7 @@ window.beerApp.IndexViewModel = (function($, Beer) { _.extend(IndexViewModel.prototype, { initialize: function(cb) { - this.doSearch((function(beers) { + this.doSearch(null, 10, (function(beers) { this.beers = this.parse(beers); cb(); }).bind(this)); @@ -59,36 +59,8 @@ window.beerApp.IndexViewModel = (function($, Beer) { this.favorites.remove( beer ); }, - doSearch: function(cb) { - // Let's pretend we do an ajax call here for the beers - setTimeout(function() { - cb([ - { - name: "Monkey King", - brewery: "New Holland", - description: "Saison", - abv: 6.6 - }, - { - name: "Lily Flagg", - brewery: "Straight to Ale", - description: "Milk Stout", - abv: 5 - }, - { - name: "Black Bavarian", - brewery: "Sprecher", - description: "Schwarzbier", - abv: 6 - }, - { - name: "Rye Stout", - brewery: "Flat 12", - description: "Stout", - abv: null - } - ]); - }, 300); + doSearch: function(query, limit, cb) { + return Beer.find(query, limit, cb); } });