finance in value chain analysis—a synthesis paper

23
Building Javascript Applications With Backbone.js Ken Harris – Sr. Developer, Telkonet.com March 13, 2012

Upload: others

Post on 12-Sep-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Building Javascript Applications With

Backbone.js

Ken Harris – Sr. Developer, Telkonet.com

March 13, 2012

Page 2: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Trends in Web Apps

● Fatter Clients ● More Javascript● More CSS● More complex● Can turn into a mess● Needs structure

Page 3: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.js

● MVC Framework for client side Javascript

● Organize and simplify JS application

● Lightweight – only 4kb minified

● Becoming popular

Page 4: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Sites using Backbone.js

● LinkedIn Mobile● Foursquare● Do● Posterous● Groupon Now!● Basecamp Mobile

● Diaspora● Blossom● Quote Roller● Salon.io● Pandora● Bit Torrent

Page 5: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Requirements

● Underscore.js – support library ● Especially for collections

● Jquery or Zepto ● Uses jquery.ajax to access server DB

● Json2.js for RESTful persistence

Page 6: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Booklist Example

Page 7: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Booklist Example

Page 8: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

MVC Design Pattern

● Model-View-Controller● Comes from Smalltalk-80● Popular pattern for server frameworks● Model = data● View = user display● Controller = Glue, accepts user interaction,

interfaces with models & dispatches views● Divide and conquer strategy

Page 9: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Events

● Mixin module – can be added to any object● Bind, trigger, and unbind● Events are strings (eg. “change”)● Trigger can pass arguments● Changes to data models can trigger automatic

refresh of views

Page 10: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Events

Var object = {};_.extend(object, Backbone.Events);

object.bind(“poke”, function(msg) {alert(“Just got poked: “+msg); });

object.trigger(“poke”, “hello...”);

Page 11: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Model

● Heart of the application● Data + logic (conversions, validations,

computed properties, access control, …)● Create new model class by extending

Backbone.Model● Create instance from class with new

Page 12: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Model

Book = Backbone.Model.extend({initialize: function() { // do something

},defaults: { loc : '', title : '', author : ''},urlRoot: '/phpdemo/db.php/'

});

Book = new Book;

Page 13: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Model

● Properties● model.id – unique server id● model.cid – client id● model.defaults – default attribute values

● Methods● Initialize() - called on creation● get(attribute) - getter● set(attributes) - setter● validate() - validation

Page 14: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Model

● More methods● fetch()● save()● destroy()● toJSON()

● Events● change, destroy, error● change event can be bound to render a view

Page 15: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Server Interaction

● Makes RESTful calls to server● CRUD (create,read,update,delete)● Create → POST server/collection/id● Read → GET server/model/id● Update → PUT● Delete → DELETE● Data passed using JSON encoding

Page 16: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Collection

● Ordered set of models

● Can listen for events on any model

● Create by extending Backbone.Collection

● Crate an instance using new

Page 17: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Collection

Booklist = Backbone.Collection.extend({ model : Book, url : '/phpdemo/db.php' });

booklist = new Booklist;

Page 18: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Collection

● Methods● add()● remove()● get(id)● getByCid(cid)● Comparator – ordering function● Uses underscore.js for iteration methods

Page 19: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.View

● Control piece that renders a view● Extend Backbone.View to create class● Use new to create an instance● View.el – associated DOM element● initialize() function● render() function● $(selector) – locally scoped jQuery

Page 20: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.View

Page 21: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Router

● Route client side pages and connect them to actions and events

● Interfaces with hashchange events● Reacts to history navigation● Need to be aware of possible server

interactions

Page 22: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

Backbone.Router

Page 23: FINANCE IN VALUE CHAIN ANALYSIS—A SYNTHESIS PAPER

References

● Javascript Web Applications, Alex MacCaw, O'Reilly (2011)

● http://documentcloud.github.com/backbone/● Backbone.js● Underscore.js● Todos demo

● Ken harris● [email protected]● @harris901