Skip to content

Updates to source to make Ajax calls #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: lab-1-git-and-grunt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ module.exports = function( grunt ) {
src: [ "assets/*.*" ],
dest: "dist/",
cwd: "src/"
},
data: {
expand: true,
src: [ "*.json" ],
dest: "dist/",
cwd: "src/"
}
},

Expand Down
44 changes: 44 additions & 0 deletions src/data.json
Original file line number Diff line number Diff line change
@@ -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
}
]
41 changes: 38 additions & 3 deletions src/js/models/beer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
window.beerApp = (window.beerApp || {});
window.beerApp.Beer = (function() {
"user strict";
window.beerApp.Beer = (function($) {
"use strict";

var nextId = 0;

Expand Down Expand Up @@ -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;

})();
})(window.jQuery);
34 changes: 3 additions & 31 deletions src/js/viewModels/indexViewModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
});

Expand Down