From 4e923c8d84a20f0e4c97b317e8736b3323f05cb3 Mon Sep 17 00:00:00 2001 From: Brian Broll Date: Wed, 13 May 2015 12:45:22 -0500 Subject: [PATCH] Refactored for modular controllers --- src/client/index.jade | 2 +- src/client/js/controllers.js | 14 +++++ src/client/js/controllers/Controller.js | 41 ++++++++++---- src/client/js/controllers/MovieController.js | 53 ++++++++++++++++++ .../js/controllers/html/.movie.html.swo | Bin 0 -> 12288 bytes src/client/js/controllers/html/movie.html | 30 ++++++++++ src/client/js/main.js | 16 ++++-- 7 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 src/client/js/controllers.js create mode 100644 src/client/js/controllers/MovieController.js create mode 100644 src/client/js/controllers/html/.movie.html.swo create mode 100644 src/client/js/controllers/html/movie.html diff --git a/src/client/index.jade b/src/client/index.jade index c1887fc..b87ab40 100644 --- a/src/client/index.jade +++ b/src/client/index.jade @@ -5,5 +5,5 @@ block content img(src="food.png") .genre#entertainmentButton(data-categories="casino shopping_mall amusement_park campground park night_club movie_rental movie_theater art_gallery stadium zoo") img(src="fun.png") - .genre#flicksButton(data-categories="movies") + .genre#flicksButton(data-categories="movies", data-view="MovieController") img(src="flicks.png") diff --git a/src/client/js/controllers.js b/src/client/js/controllers.js new file mode 100644 index 0000000..3f66d45 --- /dev/null +++ b/src/client/js/controllers.js @@ -0,0 +1,14 @@ +/*globals define*/ +define([ + './controllers/Controller', + './controllers/MovieController' +], function( + Controller, + MovieController +) { + 'use strict'; + return { + Controller: Controller, + MovieController: MovieController + }; +}); diff --git a/src/client/js/controllers/Controller.js b/src/client/js/controllers/Controller.js index ef3dbf8..5022ef6 100644 --- a/src/client/js/controllers/Controller.js +++ b/src/client/js/controllers/Controller.js @@ -20,6 +20,16 @@ define([ this._client = client; }; + /** + * Get the name of the given controller. + * + * @return {String} name + */ + Controller.prototype.getName = function() { + // Override this method when creating custom controllers + return 'Basic'; + }; + /** * Display the given option on the screen. * @@ -56,18 +66,7 @@ define([ for (var i=0; i= 0.5) { - if (option.rating >= 1) { - rating_string += ''; - option.rating -= 1; - continue; - } - if (option.rating >= 0.5) { - rating_string += ''; - option.rating -= 0.5; - } - } + var rating_string = this._createRatingText(option.rating); var user_loc = { 'latitude' : this._client.lat, 'longitude' : this._client.lng @@ -104,5 +103,23 @@ define([ console.log('No options found!'); }; + Controller.prototype._createRatingText = function(rating, total) { + var result = ''; + total = total || 5; + + while (rating >= 0.5) { + if (rating >= 1) { + result += ''; + rating -= 1; + continue; + } + if (rating >= 0.5) { + result += ''; + rating -= 0.5; + } + } + return result; + }; + return Controller; }); diff --git a/src/client/js/controllers/MovieController.js b/src/client/js/controllers/MovieController.js new file mode 100644 index 0000000..74512ba --- /dev/null +++ b/src/client/js/controllers/MovieController.js @@ -0,0 +1,53 @@ +/*globals define*/ + +define(['controllers/Controller', + 'text!controllers/html/movie.html', + 'lodash'], function(Controller, + movieHtml, + _) { + 'use strict'; + + var movieTemplate = _.template(movieHtml); + var MovieController = function() { + Controller.apply(this, arguments); + }; + + _.extend(MovieController.prototype, Controller.prototype); + + // Override methods as needed + MovieController.prototype.renderOption = function(option) { + console.log('Movie option is', option); + // Add the rating + + // Show the basic movie info + this.container.html(movieTemplate(option)); + this._updateView(option); // Using OMDB API + }; + + /** + * Get the movie info from OMDB + * + * @param {String} movie + * @return {undefined} + */ + MovieController.prototype._getMovieInfo = function(movie, cb) { + $.get('http://www.omdbapi.com/?t='+encodeURI(movie)+ + '&y=&plot=short&r=json', cb); + }; + + MovieController.prototype._updateView = function(option) { + // Update the html with the new content + this._getMovieInfo(option.title, function(movie) { + if (movie.Poster && movie.Poster.indexOf('http') === 0) { + document.getElementById('poster').innerHTML = ''; + } + + if (movie.imdbRating && parseInt(movie.imdbRating)) { + document.getElementById('rating').innerHTML = this._createRatingText(movie.imdbRating/2); + } + }.bind(this)); + }; + + return MovieController; +}); diff --git a/src/client/js/controllers/html/.movie.html.swo b/src/client/js/controllers/html/.movie.html.swo new file mode 100644 index 0000000000000000000000000000000000000000..95d3ec0ed5b5c5ce424d9bc27513c1af6659431e GIT binary patch literal 12288 zcmeI2J#W)M7{{*_us{J(u~vss$K;w4i4I987C;Pu!opxV_9eMs-&-TSagmVsFYou> zHGYsUrwI{Sn2{869JtHP!;X~Re7m@Lx(LWpt;RgGh8Y`Zo2C8<=a0CI*bN>EBa~!D zhb)W*mzIqU3zf85#WB~0jcgLLL`?*5y5%APu8;r{s3(xwLE~tLTsv$XID@O+6}o$= zS8t1_NB{{S0VIF~kN^@u0!RP}{Fel5x<#HrXWK=0d&Ru9G8bE%kN^@u0!RP}AOR$R z1dsp{Kmter2_S)gkU$U-^64xgx6eWG@c;kv@Bg0{2>A;72zmn=gIb_%(C71nyaGK3 z-39G}PJzDd5b^=^7}N*#K(|0`&}GmW(09o57W5jFfd-(5pnD)!!wV1&B!C2v01`j~ zNB{{S0VIF~{tkf-i^SxZP&!}CmK`=fC>Q0tYMQ&#e9TRXEySi_92nE}LKQcjm`2-k z$%^YTcXZy0!Vc<~-`9~Q$o2{!? zTAdVCFZH*ySprzJSBXBaIn^*J%Ht+2j`uqcQ&aqmg}|d(~+`j&9hqc z@9?hLmlaM#D5S6s?S7Y*JG#GKWu12<;CpWw^0m@x_~d<>G?1&0Hq~Ml!CfwMfiQI* zcAC1BP_P~G0D$H7uDaJ1>^%KIMt+f$xT?xg=OBZmOeJj0*TK{|LGUEdQ(8Qfqxp|u l$$0UaRo +
+
+
+
+
<%= title %>
+
+ <%= theater %> + +
+
+
<%= vicinity %>
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+ diff --git a/src/client/js/main.js b/src/client/js/main.js index c13938f..de5097c 100644 --- a/src/client/js/main.js +++ b/src/client/js/main.js @@ -25,12 +25,12 @@ require.config({ define([ 'Client' , 'Utils' , 'shake' - , './controllers/Controller' + , './controllers' , 'lodash' , 'text!../html/no_more.html' , 'text!../html/result.html' ], -function(Client, Utils, shake, Controller, _, noMoreTemplate, resultTemplate) { +function(Client, Utils, shake, Controllers, _, noMoreTemplate, resultTemplate) { // Initialize shake listening // TODO @@ -52,15 +52,19 @@ function(Client, Utils, shake, Controller, _, noMoreTemplate, resultTemplate) { }; var categories = getCategoryMap(), - client = new Client(categories); + client = new Client(categories), + controllers = []; console.log('categories:'); console.log(categories); - // Create the controllers - var Contr = Controller.bind(this, client); + // Create the controllers for the buttons + var def = 'Controller'; $('.genre').each(function(i, btn) { - new Controller(client, btn); + var name = btn.getAttribute('data-view'), + Controller = Controllers[name] || Controllers[def]; + + controllers.push(new Controller(client, btn)); });