Skip to content
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

Jessica and Gale - Carets #3

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.env

# From https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
Expand Down
36 changes: 35 additions & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,44 @@
<title>Backbone Baseline</title>
</head>
<body>
<header>
<h1>BACKBONE BASELINE RENTAL LIBRARY</h1>
</header>
<main id="main-content">

<section id="search">
<h3>Search for Movies:</h3>
<form>
<input type="text" name="search" placeholder="Search for a movie..."/>
<button class="button" type="submit" id="search-btn">Search</button>
</form>
<div id="results-container">
<h3>DATABASE RESULTS</h3>
<ul id="results-list"><!-- result-list-template renders here-->
</ul>
</div>
</section>
<section id="library">
<h3>RENTAL LIBRARY</h3>
<ul id="movie-list"><!-- library-template renders here--></ul>
</section>
</main>

<!-- UNDERSCORE TEMPLATES -->

<script type="text/template" id="result-list-template">
<img src="<%- image_url %>" />
<%- title %>
<%- release_date %>
<button class="button" id="add-btn">Add to Library</button>
</script>

<script type="text/template" id="library-template">
<img src="<%- image_url %>" />
<%- title %>
<%- release_date %>
</script>

<!-- INCLUDES -->
<script src="/app.bundle.js"></script>

</body>
Expand Down
35 changes: 31 additions & 4 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import Backbone from 'backbone';
// Import jQuery & Underscore
import $ from 'jquery';
import _ from 'underscore';

//Styles
import 'css/_settings.css';
import 'foundation-sites/dist/css/foundation.css';
import './css/styles.css';

// Import jQuery & Underscore
import $ from 'jquery';
import _ from 'underscore';
// Models & Collections
import Library from './collections/library';
import LibraryView from './views/library_view';
import ResultList from './collections/result_list';
import ResultListView from './views/result_list_view';


const library = new Library();
const resultList = new ResultList();
const bus = _.extend({}, Backbone.Events);

// ready to go
$(document).ready(function() {
library.fetch();

const libraryView = new LibraryView({
model: library,
template: _.template($('#library-template').html()),
bus: bus,
el: '#library',
});

$('#main-content').append('<p>Hello World!</p>');
libraryView.render();

const resultListView = new ResultListView({
model: resultList,
template: _.template($('#result-list-template').html()),
el: '#search',
library: library,
})
});
9 changes: 9 additions & 0 deletions src/collections/library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Backbone from 'backbone';
import Movie from '../models/movie';

const Library = Backbone.Collection.extend({
model: Movie,
url: 'http://localhost:3000/movies',
});

export default Library;
12 changes: 12 additions & 0 deletions src/collections/result_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Backbone from 'backbone';
import Movie from '../models/movie';

const ResultList = Backbone.Collection.extend({
model: Movie,
sync: function(method, model, options) {
options.url = 'http://localhost:3000/movies/search?query=' + this.query;
return Backbone.sync('read', model, options);
}
});

export default ResultList;
5 changes: 5 additions & 0 deletions src/css/styles.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@include foundation-everything;


main {
background: lightblue;
}
Expand Down Expand Up @@ -37,6 +38,10 @@ aside label {
div {
display: inline;
}

ul, li {
list-style-type: none;
}
/*
* {
border-style: solid;
Expand Down
8 changes: 8 additions & 0 deletions src/models/movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Backbone from 'backbone';

const Movie = Backbone.Model.extend({
urlRoot: 'http://localhost:3000/movies'
})


export default Movie
26 changes: 26 additions & 0 deletions src/views/library_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Backbone from 'backbone';

import MovieView from './movie_view';

const LibraryView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.model = params.model;
this.listenTo(this.model, 'update', this.render);
},
render() {
this.$('#movie-list').empty();
this.model.each((movie) => {
const movieView = new MovieView({
model: movie,
template: this.template,
tagName: 'li',
className: 'movie',
});
this.$('#movie-list').append(movieView.render().$el);
});
return this;
}
});

export default LibraryView;
23 changes: 23 additions & 0 deletions src/views/movie_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Backbone from 'backbone';

const MovieView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.model = params.model;
this.library = params.library;
},
events: {
'click #add-btn': 'addToLibrary',
},
render() {
const compiledTemplate = this.template(this.model.toJSON());
this.$el.html(compiledTemplate);
return this;
},
addToLibrary() {
this.library.add(this.model);
this.model.save();
}
});

export default MovieView;
36 changes: 36 additions & 0 deletions src/views/result_list_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Backbone from 'backbone';
import MovieView from './movie_view'

const ResultListView = Backbone.View.extend({
initialize(params) {
this.template = params.template;
this.model = params.model;
this.library = params.library;
this.listenTo(this.model, 'update', this.render);
},
events: {
'click #search-btn': 'getResults',
},
render() {
this.$('#results-list').empty();
this.model.each((movie) => {
const movieView = new MovieView({
model: movie,
template: this.template,
tagName: 'li',
className: 'movie',
library: this.library,
});
this.$('#results-list').append(movieView.render().$el);
});
return this;
},
getResults: function(event) {
event.preventDefault();
const searchTerm = this.$('[name="search"]').val();
this.model.query = searchTerm
this.model.fetch()
},
});

export default ResultListView;