Back home

BackboneJs in Drupal core

How am I?

You?

Why js matters?

Web 1.0: JS for Almost Nothing

Web 2.0: JS for AJAX

Web Today: JS for Everything

Advantages of new approach

Challenges

jQuery is not enouth

Programming best practices in front end?

Solution: Browserside js frameworks

Backbone is in Drupal Core

What is Backbone?

MV* in Bakcbone

Model

app.Todo = Backbone.Model.extend({
  defaults: {
    title: '',
    completed: false
  },

  toggle: function () {
    this.save({
      completed: !this.get('completed')
    });
  }
});

Template

var html = _.template('<li><%= name %></li>', { name: 'John Smith' });

Collection

var TodoList = Backbone.Collection.extend({
  model: app.Todo,

  localStorage: new Backbone.LocalStorage('todos-backbone'),

View

app.AppView = Backbone.View.extend({

  el: '#todoapp',

  statsTemplate: _.template($('#stats-template').html()),

  events: {
    'click #clear-completed': 'clearCompleted',
  },

  initialize: function () {
    this.$input = this.$('#new-todo');

    this.listenTo(app.Todos, 'add', this.addOne);
  }

  clearCompleted: function () { /* */},

  addOne: function () { /* */}
}

Router

var AppRouter = Backbone.Router.extend({
  routes: {
    "posts/:id": "getPost",
  }
});

var app_router = new AppRouter;
app_router.on('route:getPost', function (id) {
  alert( "Get post number " + id );
});

Backbone.history.start();

Unserscore

Front end templating

What we can use Backbone for?

Using backbone in a module

function backbone_todo_library_info() {
  $path = drupal_get_path('module', 'backbone_todo');

  $options = array(
    'scope' => 'footer',
    'attributes' => array('defer' => TRUE),
  );

  $libraries['backbone_todo'] = array(
    'title' => 'Backbone todo list',
    'version' => '0.1.0',
    'js' => array(
      $path . '/js/todo.js' => $options,
    ),

    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'underscore'),
      array('system', 'backbone'),
    )
  );

  return $libraries;
}

Presentation

Yeoman

Presantation

  1. Enable server and open page in browser
  2. Add route
  3. Add model
  4. Auto refresh

(TODO - prepare presentation)

Drupal as webservice

{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": 10021
  }
}

Questions

Materials

Summary

Contact

Credits