On Github TNBWorkshop / intro_mvc
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Tell us about yourself.
Wikiattack!: Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user. First introduced in the 1970's for use with Smalltalk-80 (wow!).
There's a lot happening in these big apps. Specifically, JavaScript:
JavaScript needed a way to organize all of this application code. Thankfully, other languages have been doing that for years.
We can do a few things to make our code more manageable:
The model represents your data.
Important Jobs:
The view is your presentation layer - what people actually see.
Important Jobs:
Typically acts as a "middleman" between your app, your views, and your data.
Its use varies in JS MVC Frameworks. More on that later.
Important Jobs:
var Model = function(data, options) { data = data || {}; options = options || {}; this.attributes = data; // initialization bits here };
Model.prototype.get = function(attrName) { // return a value from the model's attributes (data) }; Model.prototype.save = function(options) { // save bits here }; Model.prototype.delete = function() { // delete bits };
var View = function(options) { options = options || {}; this.model = options.model; this.template = options.template; // initialization bits here };
View.prototype.render = function(options) { // render bits here }; View.prototype.onDataChange = function(event) { // do stuff like re-render view on data update };
var Controller = function(route, options) { options = options || {}; this.route = route; // initialization bits here };
Controller.prototype.show = function(options) { // render bits here }; Controller.prototype.onRoute = function(event) { // when URL changes, do something // like get assoc. Models & Views, and call this.show(); };
[ { title: "Everything I Never Told You: A Novel", author: "Celeste Ng", imageUrl: "./images/books/book_1.jpeg" details: [ pages: 304, stars: 4, rating: 1 ] }, { title: "All the Light We Cannot See: A Novel", author: "Anthony Doerr", imageUrl: "./images/books/book_2.jpeg" details: [ pages: 544, stars: 4.5, rating: 2 ] }, // ... ]
<div class="book"> <h4 class="book-title">{{ title }}</h4> <div class="book-author">{{ author }}</div> <div class="book-details"> {{#each details}} <span class="detail"> {{ name }} <span class="value">{{ value }}</span> </span> {{/each}} </div> </div>
Lists of data are so common, it's useful to have a Class to handle them.
var Collection = function(data, options) { data = data || []; options = options || {}; this.model = options.model || Model; this.models = []; for(var i = 0; i < data.length; i++) { this.models.push(new this.model(data[i])); } };
Data binding is one of the most popular features of JavaScript MVC frameworks.
Automatically respond to:
Three components: Model, View, Controller.
Controllers send commands to the Model that update the Model's state. Controllers also send commands to the View that change the presentation of the model.
Three components: Model, View, Presenter.
MVP is derived from MVC.
Presenters are a middle layer, similar to a Controller. All logic related to presentation goes in the Presenters. The Model and the View don't talk to each other at all.
Examples: BackboneJS
They're quite similar, but not identical.
Three components: Model, View, ViewModel.
ViewModels send messages between the View and the Model - it is a mediator or converter. It changes Model info into View info, passes commands from the View into the Model, and exposes public properties, commands, and abstractions.
Examples: KnockoutJS
Something else! There are other types of MV* frameworks out there. They're not as popular, so don't worry about them for now, but it is good to know that there are other options out there.
Breakdown of differences between MV* frameworks.
Todo MVC implements a Todo list app in many of the popular JavaScript frameworks. It's a great tool to play with the different frameworks to see the differences.