1. Trang chủ
  2. » Công Nghệ Thông Tin

The Anatomy of BackboneJS

88 555 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề The Anatomy of BackboneJS
Trường học Standard University
Chuyên ngành Computer Science
Thể loại Luận văn
Năm xuất bản 2023
Thành phố New York
Định dạng
Số trang 88
Dung lượng 7,81 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Giải phẫu "Xương sống"

Trang 2

L E V E L 1

Trang 3

-{ description: 'Pick up milk', status: 'incomplete', id: 1 }

$ getJSON( '/todo' , function (data) {

To get the todo data

The data returned

Introducing the Todo App

Trang 4

Checking off a todo item

Trang 6

“Get your truth out of the DOM”

Introducing Backbone.js

- Jeremy Ashkenas

Provides client-side app structure

Models to represent data

Views to hook up models to the DOM

Synchronizes data to/from server

Trang 7

Server Client

DOM

With Backbone.js

var todoItem = new TodoItem(

{ description: 'Pick up milk', status: 'incomplete', id: 1 });

var TodoItem = Backbone.Model.extend({});

To create a model class

To create a model instance

Trang 8

var todoItem = new TodoItem(

{ description: 'Pick up milk', status: 'incomplete', id: 1 });

Models

Trang 9

Displaying the Data

var todoView = new TodoView({ model: todoItem });

To create a view class

To create a view instance

Views

Builds the HTML Provides the data

var TodoView = Backbone.View.extend({});

Trang 10

Rendering the View

var TodoView = Backbone.View.extend({

Every view has a top level EL ement

<p> <li> <header> <section>

<div>

default

Trang 11

Rendering the View

var TodoView = Backbone.View.extend({

Trang 12

L E V E L 2

Trang 13

var todoItem = new TodoItem(

{ description: 'Pick up milk', status: 'incomplete' });

var TodoItem = Backbone.Model.extend({});

Generating a model instance

Generating a model class

Trang 14

Fetching Data from the Server

DOM

var todoItem = new TodoItem();

To populate model from server

URL to get JSON data for model

todoItem.fetch();

todoItem.url = '/todo';

todoItem.get('description');

'Pick up milk'

/todo isn’t a good URL

{ id: 1, description: 'Pick up milk' , status: 'incomplete' }

Trang 15

Fetching Data from the Server

Fetch todo with id = 1

todoItem.fetch();

{ id: 1, description: 'Pick up milk' , status: 'incomplete' }

var TodoItem = Backbone.Model.extend({urlRoot: '/todos'});

var todoItem = new TodoItem({id: 1})

RESTful web service (Rails flavor)

GET /todos/1

todoItem.set({description: 'Pick up cookies.'});

Update the todo

with JSON params

Trang 16

Creating & Destroying a New Todo

2

var todoItem = new TodoItem();

todoItem.set({description: 'Fill prescription.'});

with JSON params

todoItem.get('id');

todoItem.toJSON();

{ id: 2, description: 'Fill prescription' , status: 'incomplete' }

Get JSON from model

Trang 18

Models Can Have Events

todoItem.trigger('event-name');

todoItem.on('event-name', function(){ alert('event-name happened!');

});

Run the event

To listen for an event on a model

Trang 19

Special Events

todoItem.on('change', doThing);

To listen for changes

todoItem.set({description: 'Fill prescription.'});

Event triggered on change

todoItem.set({description: 'Fill prescription.'},

{silent: true});

Set without triggering event

todoItem.off('change', doThing);

Remove event listener

var doThing = function() {

}

Trang 20

Special Events

todoItem.on(<event>, <method>);

Built-in events

change:<attr> When <attr> is modified

sync Whenever successfully synced

all Any triggered event

Trang 21

L E V E L 3

Trang 22

-More on the View Element

console.log(simpleView.el);

var SimpleView = Backbone.View.extend({});

var simpleView = new SimpleView();

<div></div>

console.log(simpleView.el);

var SimpleView = Backbone.View.extend({tagName: 'li'});

var simpleView = new SimpleView();

<li></li>

tagName can be any HTML tag

Trang 23

More on the View Element

var TodoView = Backbone.View.extend({

Trang 24

More on the View Element

I want to use a jQuery method

var todoView = new TodoView();

Trang 26

Adding the EL attributes

var TodoView = Backbone.View.extend({

Trang 30

Adding View Events

In jQuery to add an alert on click

Not how we do things in Backbone

Trang 31

View Events

var TodoView = Backbone.View.extend({

alertStatus: function(e){

alert('Hey you clicked the h3!');

Selector is scoped to the el

"<event> <selector>": "<method>"

Trang 32

Views Can Have Many Events

var DocumentView = Backbone.View.extend({

events: {

"dblclick" : "open",

"click icon.doc" : "select",

"click show_notes" : "toggleNotes",

"click title lock" : "editAccessLevel", "mouseover title date" : "showTooltip"

Trang 33

View Event Options

var SampleView = Backbone.View.extend({

dbclickkeydownmouseleaveready

focuskeypressmousemoveresize

focusinloadmouseoutscroll

Trang 34

Models & Views

L E V E L 4

Trang 35

-Review our Model View

}

render: function(){

this.$el.html(this.template(this.model.toJSON()));

todoView.render();

Trang 36

Adding a checkbox

});

var TodoView = Backbone.View.extend({

<%= description %></h3>'),template: _.template('<h3>

}

render: function(){

this.$el.html(this.template(this.model.toJSON()));

' +'<input type=checkbox ' +

'<% if(status === "complete") print("checked") %>/>' +

'

How do we update the model when

checkbox changes?

Trang 37

View events update the Model

});

}

Build the HTML Provides the Data

DOM event Update the data

Server

Data

Server

Data

Trang 38

Update model on UI event

if(this.model.get('status') === 'incomplete'){

this.model.set({'status': 'complete'});

Trang 39

Refactor to the Model

if(this.get('status') === 'incomplete'){

this.set({'status': 'complete'});

Trang 40

Sync changes to server

if(this.get('status') === 'incomplete'){

this.set({'status': 'complete'});

Trang 41

Update view to reflect changes

.complete { color : #bbb ; text-decoration : line-through ; }

template: _.template('<h3 class="<%= status %>">' +

'<% if(status === "complete") print("checked") %>/>' +

' <%= description %></h3>')

update TodoView template:

How should we update the view

when the model changes?

Trang 42

Re-render the view

this.$el.html(this.template(this.model.toJSON()));

Doesn’t work for other model changes

Trang 43

Model updates change the View

DOM event Update the data

Notify changed Re-renders

Use Model Events

Trang 44

Re-render the view

} this.$el.html(this.template(this.model.toJSON()));

Why the third argument?

Trang 47

Remove view on model destroy

},this.$el.html(this.template(this.model.toJSON()));

this.model.on('destroy', this.remove, this);

remove: function(){

this.$el.remove();

}

Trang 48

Watch it in action

Trang 49

L E V E L 5

Trang 50

-Set of Models

var todoItem1 = new TodoItem();

var todoItem2 = new TodoItem();

var todoList = [todoitem1, todoItem2];

TodoList manages a set of TodoItem model instances

var TodoList = Backbone.Collection.extend({ model: TodoItem

});

var todoList = new TodoList();

Trang 51

add a model instance

get model instance at index 0

removing a model instance

todoList.remove(todoItem1);

todoItem1

todoItem1

Trang 52

Each object in Array becomes a TodoItem

todoList.reset(todos);

var todos = [

{description: 'Pick up milk.', status: 'incomplete'}, {description: 'Get a car wash', status: 'incomplete'}, {description: 'Learn Backbone', status: 'incomplete'}];

Trang 53

Fetching Data from the Server

var TodoList = Backbone.Collection.extend({

2

todoList.length;

GET /todos

Trang 54

Collections Can Have Events

todoList.trigger('event-name');

todoList.on('event-name', function(){

alert('event-name happened!');

});

Run the event

To listen for an event on a collection

Works just like models!

Trang 55

Special Events

listen for reset

Event triggered on reset & fetch

without notification

todoList.off('reset', doThing);

Remove event listener

var doThing = function() {

}todoList.on('reset', doThing);

todoList.fetch();

todoList.reset();

todoList.fetch({silent: true});

todoList.reset({silent: true});

Trang 56

Special Events on CollectiontodoList.on(<event>, <function>);

Built-in Events

add When a model is added

todoList.on('add', function(todoItem){

});

todoItem is the model being added

Trang 57

Model Events

Models in collection

Events triggered on a model in a

collection will also be triggered on

the collection

change:<attr> When <attr> is modified

sync Whenever successfully synced

all When an attribute is modified

Trang 58

todoList.reset([

{description: 'Pick up milk.', status: 'incomplete', id: 1}, {description: 'Get a car wash.', status: 'complete', id: 2}]);

Setup our collection

Alert each model’s description

todoList.forEach(function(todoItem){

alert(todoItem.get('description'));

});

Trang 59

Iteration continued

Build an array of descriptions

todoList.map(function(todoItem){

return todoItem.get('description');

});

['Pick up milk.', 'Get a car wash']

Filter models by some criteria

todoList.filter(function(todoItem){

return todoItem.get('status') === "incomplete";});

Returns array of items

that are incomplete

Trang 60

Other Iteration functions

http://documentcloud.github.com/backbone/#Collection-Underscore-Methods

chain

Trang 61

Collections & Views

L E V E L 6

Trang 62

-Todo List!

Collection + View == Collection View!

Trang 63

Review our Model View

var todoItem = new TodoItem();

var todoView = new TodoView({model: todoItem});

console.log(todoView.render().el);

}

});

Trang 64

Collection View 1 to many

A Collection View doesn’t render any of it’s own HTML

It delegates that responsibility to the model views.

Trang 65

Define and Render

var TodoListView = Backbone.View.extend({});

var todoListView = new TodoListView({collection: todoList});

forEach changes context

this.collection.forEach(function(todoItem){

Trang 68

Adding new Models

newTodoItem not in DOM

var newTodoItem = new TodoItem({

description: 'Take out trash.',

status: 'incomplete'

});

todoList.add(newTodoItem);

var TodoListView = Backbone.View.extend({

addOne: function(todoItem){

var todoView = new TodoView({model: todoItem}); this.$el.append(todoView.render().el);

Trang 69

Listen to the add Event

var TodoListView = Backbone.View.extend({

initialize: function(){

this.collection.on('add', this.addOne, this); },

var newTodoItem = new TodoItem({

description: 'Take out trash.',

status: 'incomplete'

});

todoList.add(newTodoItem);

addOne: function(todoItem){

var todoView = new TodoView({model: todoItem}); this.$el.append(todoView.render().el);

Trang 70

Add Event in Action

Trang 71

Reset Event

var todoList = new TodoList();

var todoListView = new TodoListView({

addOne: function(todoItem){

var todoView = new TodoView({model: todoItem}); this.$el.append(todoView.render().el);

},

});

Trang 72

addOne: function(todoItem){

var todoView = new TodoView({model: todoItem}); this.$el.append(todoView.render().el);

Trang 73

Reset Event in Action

removing from collection

Trang 74

Fixing remove with Custom EventstodoList.remove(todoItem);

Trang 75

Bringing it all together

Trang 76

Router and History

L E V E L 7

Trang 77

-The Problem

<a href='#' class='todo'></a>

$('a.todo').click(function(e){ e.preventDefault();

// show single todo

})

Trang 78

The Problem

Router & History to the rescue!

Trang 79

The Router

Router’s map URLs to actions

var router = new Backbone.Router({routes: { "todos": 'index' },

Trang 81

More Route Matchers

* Wildcard matches everything

after file/

search/:query/p:page search/ruby/p2 query = ‘ruby’, page = 2

folder/:name-:mode folder/foo-r name = ‘foo’, mode = ‘r’

file/*path file/hello/world.txt path = ‘hello/world.txt’

Trang 82

<a href='#todos/1'> ☞ </a>

These won’t work yet

Trang 83

});

Hashbangs (#) HTML5 pushState

Trang 84

Backbone.history.start();

router.navigate("todos/1")

pushState off

#todos/1

router.navigate("todos/1") /todos/1

Backbone.history.start({pushState: true});

pushState on

Trang 85

routes: {

Show Action

var todoList = new TodoList();

var TodoApp = new TodoRouter({todoList: todoList});

Define router class

Instantiate router instance

var TodoRouter = Backbone.Router.extend({

show: function(id){

Trang 86

routes: {

Index Action

var TodoRouter = Backbone.Router.extend({

show: function(id){

this.todoList.focusOnTodoItem(id); },

initialize: function(options){

Trang 87

In Action

});

Trang 88

App Organization

var TodoApp = new (Backbone.Router.extend({

routes: { "": "index", "todos/:id": "show" },

initialize: function(){

this.todoList = new TodoList();

this.todosView = new TodoListView({collection: this.todoList});

Ngày đăng: 17/01/2013, 22:48

TỪ KHÓA LIÊN QUAN

w