From 12b2fd1ae7b53c9e4c9804c40968f00e6aded9ea Mon Sep 17 00:00:00 2001 From: jakerella Date: Sun, 1 Mar 2015 12:16:40 -0500 Subject: [PATCH] Added 'find' method to beer model; added ajax call to search with json file --- src/data.json | 44 +++++++++++++++++++++++++++++ src/js/models/beer.js | 41 ++++++++++++++++++++++++--- src/js/viewModels/indexViewModel.js | 36 +++-------------------- 3 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 src/data.json 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 0e01d17..b61cfdc 100755 --- a/src/js/models/beer.js +++ b/src/js/models/beer.js @@ -1,7 +1,5 @@ window.beerApp = (window.beerApp || {}); -window.beerApp.Beer = (function() { - "user strict"; - +window.beerApp.Beer = (function($) { var nextId = 0; var Beer = function( options ) { @@ -28,6 +26,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); diff --git a/src/js/viewModels/indexViewModel.js b/src/js/viewModels/indexViewModel.js index 0501068..3a31d20 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)); @@ -58,37 +58,9 @@ window.beerApp.IndexViewModel = (function($, Beer) { removeFromFavorites: function( beer ) { this.favorites = _.without(this.favorites, 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); } });