Introduction to Backbone.js

21
introduction & patterns Backbone.js

description

Basics of the Backbone.js framework.

Transcript of Introduction to Backbone.js

Page 1: Introduction to Backbone.js

introduction & patterns

Backbone.js

Page 2: Introduction to Backbone.js

What is backbone.js

A Javascript MVC framework?

Page 3: Introduction to Backbone.js

What is backbone.js

Almost.

Page 4: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 5: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 6: Introduction to Backbone.js

Model

var Todo = Backbone.Model.extend({

urlRoot: “/api/todos”,

toggle: function() {this.save({done: !this.get("done")});

}});

var todo = new Todo({“id”: “1”});todo.fetch();todo.toggle();

Page 7: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 8: Introduction to Backbone.js

Collection

var Todos = Backbone.Collection.extend({

url: “/api/todos”,

model: Todo,});

var todos = new Todos();todos.fetch();

var todo = todos.get(“1”);todo.toggle();

Page 9: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 10: Introduction to Backbone.js

Events

var todos = new Todos();

todos.on(“change”, function(eventName, this) {console.log(“Todos changed”);

}, todos);

todos.fetch();

Page 11: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 12: Introduction to Backbone.js

Viewvar TodoView = Backbone.View.extend({

tagName: “li”, events: { “click .toggle”: “toggle”, },

initialize: function() {this.listenTo(this.model, “change”, this.render);

}, toggle: function() { this.model.toggle(); }

render: function() {this.$el.html(“<p><i class=’toggle’/>” + this.model.get(“name”) + ”

</p>”);}

});

Page 13: Introduction to Backbone.js

View

var TodosView = Backbone.View.extend({initialize: function() {

this.listenTo(this.collection, “change”, this.render);},render: function() {

_.each(this.collection, function(todo) {this.$el.append(new TodoView({model: todo}).render());

});}

});

var todos = new Todos();var todosView = new TodosView({collection: todos});todos.fetch();

Page 14: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 15: Introduction to Backbone.js

Template

// underscore.js !

var TodoView = Backbone.View.extend({…template: _.template(...);render: function() {

this.$el.html(this.template(this.model.attributes));return this;

}});

Page 16: Introduction to Backbone.js

What is backbone.js

Collection

Model

View

Router

Sync EventsSERVER

BROWSER

CTRL

Template

Page 17: Introduction to Backbone.js

Router

var app = Backbone.Router.extend({routes: {

“/todos”: “todos”, // #/todos“/todos/:id”: “todo” // #/todos/1

},

todos: function() {...

},todo: function(id) {

...}

});

Page 18: Introduction to Backbone.js

This is a lot of boilerplate code.

Page 19: Introduction to Backbone.js

Fortunately backbone.js supports inheritance!

Page 20: Introduction to Backbone.js

Backbone patterns.

Page 21: Introduction to Backbone.js

Thank you

● @theorm

● https://github.com/theorm/backbone-recipes