mvc & backbone.js

23
MVC & backbone.js April 18 2012 Mohammed Arif Manager Interactive Development @ SapientNitro www.mohammedarif.com https://twitter.com/#!/arif_iq http://in.linkedin.com/in/mohdarif

Upload: mohammed-arif

Post on 15-Jan-2015

2.492 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: MVC & backbone.js

MVC&

backbone.js

April182012

Mohammed ArifManager Interactive Development @ SapientNitro

www.mohammedarif.comhttps://twitter.com/#!/arif_iqhttp://in.linkedin.com/in/mohdarif

Page 2: MVC & backbone.js

Agenda

• What is MVC

• Why MVC

• MVC Advantages

• What is backbone

• Why backbone

• What is JavaScript templating

• Why JavaScript templating

• Questions/Feedback

Page 3: MVC & backbone.js

What is MVC…

MVC stands for Model-View-Controller.

Page 4: MVC & backbone.js

What is MVC…

MVC is a Architectural Design Pattern

Page 5: MVC & backbone.js

What is MVC…

Separates a web application into three parts: the data (Model), the presentation of that data to the user (View), and the actions taken on any user interaction (Controller or routers).

Page 6: MVC & backbone.js

What is MVC

In the past it has been heavily used for structuring desktop and server-side applications, but it's only been in recent years that come to being applied to JavaScript.

As the majority of JavaScript developers currently using these patterns opt to utilize libraries such as Backbone.js for implementing an MVC/MV*-like structure.

Page 7: MVC & backbone.js

Design Pattern

A pattern that has been developed to help programmers cope with common problems

Blueprints on how to construct something

Page 8: MVC & backbone.js

MVC - Model

Data representation

Can be database/xml/etc

Business Logic

Page 9: MVC & backbone.js

MVC - View

Data presentation and user input

Renders the Model in to a View

Can be HTML/PDF/WML/Javascript

No computations, very little logic, display logic i.e. loops

Page 10: MVC & backbone.js

MVC - Controller

Dispatches Requests and controls flow

Centralizes access

Interacts with Model and View

Page 11: MVC & backbone.js

Why MVC

If you do a lot of JavaScript, things tend to get messy!

Backbone provides a way to organize your code, by using the MVC pattern

Only the View accesses the DOM (e.g. with jQuery, Dojo,…)

Page 12: MVC & backbone.js

MVC Advantages…

Separation of interests

Model centralizes business logic

View centralizes display logic

Controller centralizes application flow

Page 13: MVC & backbone.js

MVC Advantages

Clean separation of content/style

Improved decoupling

Easier testing

Allow multiple people to work on different parts

Page 14: MVC & backbone.js

What is backbone.js

Backbone.js is one of a number of JavaScript frameworks for creating MVC-like web applications, it's relatively lightweight and can be easily tested using third-party toolkits such as Jasmine or QUnit.

Or

Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Page 15: MVC & backbone.js

Why backbone

Organize the structure to your application

Simplify server-side persistence

Decouple the DOM from your page's data

Model data, views and routers in a succinct manner

Provide DOM, model and collection synchronization

Page 16: MVC & backbone.js

Backbone.js - Model

Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control.

Page 17: MVC & backbone.js

Backbone.js - Model

/*Let’s create a model*/

Person = Backbone.Model.extend({

initialize: function(){

alert("Hello Studio");

}

});

var person = new Person;/*initialize() is triggered whenever you create a new instance of a model*/

Page 18: MVC & backbone.js

Backbone.js - View

Backbone views are used to reflect what your applications’ data models look like

They are also used to listen to events and react accordingly

Page 19: MVC & backbone.js

Backbone.js - View

SearchView = Backbone.View.extend({

initialize: function(){

alert("Alerts suck.");

}

});

// The initialize function is always called when instantiating a Backbone View.

// Consider it the constructor of the class.

var search_view = new SearchView;

Page 20: MVC & backbone.js

Backbone.js - Collection

Backbone collections are simply an ordered set of models.

var Song = Backbone.Model.extend({

initialize: function(){

console.log("Music is the answer");

}

});

var Album = Backbone.Collection.extend({

model: Song

});

Page 21: MVC & backbone.js

What is JavaScript templating

A template contains markup with binding expressions. The template is applied to data objects or arrays, and rendered into the HTML DOM.

Page 22: MVC & backbone.js

Why JavaScript templating

Loading all data from the server especially in rich list displays

Adding or updating new items in lists

Anywhere you need to add new complex content to the page

Anything that requires client side HTML rendering

Page 23: MVC & backbone.js