BB101: Basic View /w Model

by kyllle

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css">
<div class="js-app"></div>

<script type="text/template" class="tmpl-movie">
    <a href="/movie/<%= id %>" class="movie">
    	<img src="http://image.tmdb.org/t/p/w500/<%= poster_path %>" alt="<%= title %>">
    	<h2 class="movie__info"><%= title %></h2>
	</a>
</script>

SCSS

* {
  -webkit-font-smoothing: antialiased;
}

.movie {
    display: block;
    position: relative;
    color: black;
    background: black;
    overflow: hidden;

    &__info {
        padding: 8px;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        background: linear-gradient(to top, rgba(0,0,0,0) 0%,rgba(0,0,0,1) 100%);
        color: white;
    }

    img {
        width: 100%;
        display: block;
    }

	h2 {
        margin: 0;
    }
}

Babel + JSX

console.clear();

const data = {
    "id": 140607,
    "overview": "A continuation of the saga created by George Lucas, set thirty years after Star Wars: Episode VI – Return of the Jedi.",
    "release_date": "2015-12-18",
    "poster_path": "/fYzpM9GmpBlIC893fNjoWCwE24H.jpg",
    "title": "Star Wars: Episode VII - The Force Awakens",
    "vote_average": 7.8
};

// Classes.
const Movie = Backbone.Model.extend();

const MovieView = Backbone.View.extend({

	template: _.template($('.tmpl-movie').html()),
    
    render() {
    	this.$el.html(this.template(this.model.toJSON()));
        return this;
    }
});

// Instances.
const movie = new Movie();
const movieView = new MovieView();

$('.js-app').html( movieView.render().el );