Using Backbone.js with Drupal 7 and 8 VADIM MIRGOROD Front-end, - - PowerPoint PPT Presentation

using backbone js with drupal 7 and 8
SMART_READER_LITE
LIVE PREVIEW

Using Backbone.js with Drupal 7 and 8 VADIM MIRGOROD Front-end, - - PowerPoint PPT Presentation

Using Backbone.js with Drupal 7 and 8 VADIM MIRGOROD Front-end, 05/22/2013 Building Bridges, Connecting Communities Vadim Mirgorod Lead developer in Trellon Writing Backbone.js CookBook for PACKT Email: dealancer@gmail.com Web:


slide-1
SLIDE 1

Building Bridges, Connecting Communities

VADIM MIRGOROD

Front-end, 05/22/2013

Using Backbone.js with Drupal 7 and 8

slide-2
SLIDE 2

Vadim Mirgorod

  • Lead developer in Trellon
  • Writing Backbone.js CookBook for PACKT
  • Email: dealancer@gmail.com
  • Web: http://vmirgorod.name
  • Twitter: @dealancer

#2

slide-3
SLIDE 3

Remember the web development in 90s?

#3

slide-4
SLIDE 4

1995: JavaScript

#4

slide-5
SLIDE 5

2000: XMLHttpRequest

#5

slide-6
SLIDE 6

2006: jQuery

#6

slide-7
SLIDE 7

2013:

?

#7

slide-8
SLIDE 8

What's new in JS?

  • HTML5:

– Local Storage – pushState

  • JS templating engines:

– Mustache.js – Twig.js

  • Representative State Transfer (REST)

#8

slide-9
SLIDE 9

JavaScript client evolution

#9

slide-10
SLIDE 10

Complexity

#10

slide-11
SLIDE 11

Thin to thick transition

#11

slide-12
SLIDE 12

Performance

#12

slide-13
SLIDE 13

Problems!

slide-14
SLIDE 14

Right now typical AJAX code looks like this

$(document).ready(function() { $("#getData").click( function() { $.getJSON("artistsRemote.cfc?method=getArtists&returnformat=json", function(data) { $("#artistsContent").empty(); $("#artistsTemplate").tmpl( data ).appendTo("#artistsContent"); var nowIs = new Date().toLocaleString(); $('#lastLoad').html( nowIs ); }); }); }); #14

slide-15
SLIDE 15

AJAX in Drupal

Have you seen any JS?

#15

slide-16
SLIDE 16

#16

slide-17
SLIDE 17

Let's do things properly!

#17

slide-18
SLIDE 18

Meet...

#18

slide-19
SLIDE 19

#19

slide-20
SLIDE 20

Backbone.js

  • URL: http://backbonejs.org/
  • Created by Jeremy Ashkenas in 2010, an

author of CoffeeScript

  • Based on Underscore.js:

http://backbonejs.org/

  • Requires jQuery or Zepto

#20

slide-21
SLIDE 21

Backbone.js features

  • Minimalistic
  • Modular
  • Perfect OOP design
  • Over 100 available extensions:

https://github.com/documentcloud/backbone/wik i/Extensions,-Plugins,-Resources

  • Community

#21

slide-22
SLIDE 22

Backbone vs...

  • Knockout
  • AngularJS
  • CanJS
  • Spine
  • Ember.js
  • many others

#22

slide-23
SLIDE 23

Backbone vs...

#23

slide-24
SLIDE 24

Who uses Backbone.js?

  • Groupon Now!
  • Foursquare
  • LinkedIn Mobile
  • Airbnb

#24

slide-25
SLIDE 25

Let's learn how to backbone!

#25

slide-26
SLIDE 26

Main concepts

  • Model
  • Collection
  • View
  • Template
  • Router
  • History
  • Events

#26

slide-27
SLIDE 27

MVC: Backbone:

http://www.jboss.org/jdf/examples/ticket-monster/tutorial/UserFrontEnd/ #27

slide-28
SLIDE 28

DEMO! TODO APP

#28

slide-29
SLIDE 29

Easy example

slide-30
SLIDE 30

Define the model and the collection

// Define new model. var InvoiceItemModel = Backbone.Model.extend({ calculateAmount: function() { return this.get('price') * this.get('quantity'); } }); // Define new collection object. var InvoiceItemCollection = Backbone.Collection.extend({ model: InvoiceItemModel }); #30

slide-31
SLIDE 31

Define the view to render the model

var InvoiceItemView = Backbone.View.extend({ // Define element tag name. tagName: 'tr', // Define template. template: _.template($('#item-row-template').html()), // Render the view. render: function() { var data = this.model.toJSON(); data.amount = this.model.calculateAmount(); $(this.el).html(this.template(data)); return this; }, }); #31

slide-32
SLIDE 32

Define the view to render the collection

var InvoiceItemListView = Backbone.View.extend({ tagName: 'table', template: _.template($('#item-table-template').html()), // Render the view. render: function() { $(this.el).html(this.template()); _.each(this.collection.models, function(model, key) { this.append(model); }, this); return this; }, // Add an invoice item row to the table. append: function(model) { $(this.el).append(new InvoiceItemView({ model: model }).render().el); } });

#32

slide-33
SLIDE 33

Add templates into index.html

<script type="text/html" class="template" id="item-table-template"> <tr> <th>Quantity</th> <th>Description</th> <th>Price</th> <th>Amount</th> </tr> </script> <script type="text/html" class="template" id="item-row-template"> <td><%= quantity %></td> <td><%= description %></td> <td><%= price %></td> <td><%= amount %></td> </script> #33

slide-34
SLIDE 34

Create collection and show the view

var invoiceItemCollection = new InvoiceItemCollection([ { description: 'Wooden Toy House', price: 22, quantity: 3 }, { description: 'Farm Animal Set', price: 17, quantity: 1 }, ]); $('body').html(new InvoiceItemListView({ collection: invoiceItemCollection }).render().el ); #34

slide-35
SLIDE 35

Some cool things

Two way binding Model to view binding Layouts Templates Router Forms

slide-36
SLIDE 36

Model to view binding

var InvoiceItemView = Backbone.View.extend({ // ... initialize: function() { this.listenTo(this.model, 'destroy', this.destroy, this); this.listenTo(this.model, 'change', this.render, this); }, destroy: function() { this.remove(); } }); var InvoiceItemListView = Backbone.View.extend({ // ... initialize: function() { this.listenTo(this.collection, 'add', this.append, this); } }); #36

slide-37
SLIDE 37

Forms

// Backbone-forms extension. var UserModel = Backbone.Model.extend({ schema: { title: { type: 'Select', options: ['Mr', 'Mrs', 'Ms'] }, name: 'Text', email: { validators: ['required', 'email'] }, password: 'Password', birthday: 'Date', } }); userModel = new BuyerModel(); $('body').append(new Backbone.Form({ model: this.user }).form.render().el); #37

slide-38
SLIDE 38

Two-way binding

// Backbone.stickit extension. var InvoiceItemFormView = Backbone.View.extend({ className: 'invoice-item-form-view', bindings: { '#description': 'description', '#price': 'price', '#quantity': 'quantity' }, render: function() { var html = '<label>Description:</label><input type="text" id="description"></input><br>' + '<label>Price:</label><input type="text" id="price"></input><br>' + '<label>Quantity:</label><input type="text" id="quantity"></input><br>'; $(this.el).html(html); // Here binding occurs. this.stickit(); return this; }

#38

slide-39
SLIDE 39

Forms

// Backbone-forms extension. var UserModel = Backbone.Model.extend({ schema: { title: { type: 'Select', options: ['Mr', 'Mrs', 'Ms'] }, name: 'Text', email: { validators: ['required', 'email'] }, password: 'Password', birthday: 'Date', } }); userModel = new BuyerModel(); $('body').append(new Backbone.Form({ model: this.user }).form.render().el); #39

slide-40
SLIDE 40

Router

var Workspace = Backbone.Router.extend({ routes: { // Default path. '': 'invoiceList', // Usage of static path. 'invoice': 'invoiceList', // Usage of fragment parameter. 'invoice/:id': 'invoicePage', // Usage of fragment parameters. 'help/:topic/page:page': 'helpPage', // Usage of splat parameter. 'download/*path': 'downloadPage' }, }); #40

slide-41
SLIDE 41

Other cool things...

in Backbone.js Cookbook

slide-42
SLIDE 42
slide-43
SLIDE 43
  • Bootstrapping:

– Technique we saw above – Do it yourself

  • Representational State Transfer:

– Services module – RESTful webservices module

  • Backbone.js module

Backbone and Drupal 7

#43

slide-44
SLIDE 44

REST

#44

slide-45
SLIDE 45

REST in Backbone

var PostModel = Backbone.Model.extend({ // Override id attribute. idAttribute: '_id', // Define URL root to access model resource. Otherwise use // url() method to provide full path to the model resource. urlRoot: function() { return 'http://example.com/posts/'; } }); var PostCollection = Backbone.Collection.extend({ model: PostModel, // Define path to the collection resource. url: function() { return 'http://example.com/posts/'; } }); #45

slide-46
SLIDE 46

REST in Backbone.js

// Fetches data into a model. model.fetch(); // Saves a model. model.save(); // Destroys a model. model.destroy(); // Fetches data into a collection. collection.fetch(); // Adds models to a collection. collection.add(models); // Removes specific models from collection. collection.remove(models); #46

slide-47
SLIDE 47

Backbone module

  • URL: http://drupal.org/project/backbone
  • Provides models and collections for Drupal

entities via REST: – Node: Node model – All node: NodeIndex collection – Arbitrary view: NodeView collection

  • Works with both Services and RESTful Web

Services modules.

#47

slide-48
SLIDE 48

Backbone module

// Create new NodeView collection. var viewCollection = new Drupal.Backbone.Collections.NodeView(); // Set Drupal View name. viewCollection.viewName = 'backbone_example'; // Fetch data from the collection. viewCollection.fetch({success: function() { console.log(viewCollection.toJSON()); });

#48

slide-49
SLIDE 49

Backbone mdl roadmap

  • Support for bootstrapping
  • Better views support
  • In-place content editing
  • Drag and Drop
  • D8 version?

#49

slide-50
SLIDE 50

http://www.flickr.com/photos/gabblebee/3977912731/

slide-51
SLIDE 51

Backbone and Drupal 8

  • It is in core!
  • Used for In-place editing issue:

http://drupal.org/node/1824500

  • Used for layouts issue:

http://drupal.org/node/1841584

  • Used for toolbar issue:

http://drupal.org/node/1860434

  • META issue: http://drupal.org/node/1858368

#51

slide-52
SLIDE 52

DEMO!

slide-53
SLIDE 53

One more thing!

  • Web services initiative

– REST in core – Storage controllers

  • Twig

– Templating engine – Twig in core (Twig sandbox) – Works both for PHP and JS

#53

slide-54
SLIDE 54

One more thing!

#54

Twig templates Storage controllers Access controllers Render controllers Form controllers DB Browser

slide-55
SLIDE 55

One more thing!

#55

Backbone app Twig templates Storage controllers Access controllers Render controllers Form controllers DB REST Browser

slide-56
SLIDE 56

One more thing!

#56

Backbone app Twig templates Storage controllers Access controllers Render controllers Form controllers DB REST Browser Mobile

slide-57
SLIDE 57

One more thing!

#57

Backbone app Twig templates Storage controllers Access controllers DB REST Mobile app built with PhoneGap or Trigger.io

slide-58
SLIDE 58

Building Bridges, Connecting Communities

Evaluate this session at: portland2013.drupal.org/node/1578.

Thank you!

What did you think?