31
BackboneJs in Drupal core DrupalCamp Wroclaw, 14 April, 2013

Backbone js in drupal core

Embed Size (px)

Citation preview

Page 1: Backbone js in drupal core

BackboneJs in Drupal core

DrupalCamp Wroclaw, 14 April, 2013

Page 2: Backbone js in drupal core

Who am I?

Page 3: Backbone js in drupal core

You?

Page 4: Backbone js in drupal core

Why js matters?

Page 5: Backbone js in drupal core

Web 1.0: JS for Almost Nothing

Page 6: Backbone js in drupal core

Web 2.0: JS for AJAX

Page 7: Backbone js in drupal core

Web Today: JS for Everything

Page 8: Backbone js in drupal core

Advantages of new approach

Server

Client

js & templates RESTJSON

Page 9: Backbone js in drupal core

Challenges

Page 10: Backbone js in drupal core

jQuery is not enough

Page 11: Backbone js in drupal core

Programming best practices in front end?

Page 12: Backbone js in drupal core

Solution: Browserside js frameworks

Page 13: Backbone js in drupal core

Backbone is in Drupal Core

Page 14: Backbone js in drupal core

What is Backbone?

?

Page 15: Backbone js in drupal core

MV* in Backbone

Page 16: Backbone js in drupal core

Modelapp.Todo = Backbone.Model.extend({

defaults: {

title: '',

completed: false

},

toggle: function () {

this.save({

completed: !this.get('completed')

}); } });

Page 17: Backbone js in drupal core

Templatevar html =

_.template('<li><%= name %></li>'

, { name: 'John Smith' });

Page 18: Backbone js in drupal core

Collectionvar TodoList = Backbone.Collection.extend({

model: app.Todo,

localStorage: new Backbone

.LocalStorage('todos-backbone'),

Page 19: Backbone js in drupal core

Viewapp.AppView = Backbone.View.extend({

el: '#todoapp',

statTemplate: _.template($('#id').html()),

events: {

'click #clear': 'clear'},

initialize: function () {

this.$input = this.$('#new-todo');

this.listenTo(app.Todos, 'add', this.

addOne);

}

Page 20: Backbone js in drupal core

Routingvar 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();

Page 21: Backbone js in drupal core

Underscore

Page 22: Backbone js in drupal core

Front end templating

● allows us to dynamically build html on browser side

● non-hacky approach to ajax

Page 23: Backbone js in drupal core

What we can use Backbone for?

Internal app

Admin panel

Page 24: Backbone js in drupal core

Using Backbone in modulefunction backbone_todo_library_info() {

$libraries['backbone_todo'] = array(

'js' => array(

$path . '/js/todo.js' => $options,

),

'dependencies' => array(

array('system', 'jquery'),

array('system', 'underscore'),

array('system', 'backbone'),

));

Page 25: Backbone js in drupal core

Yeoman

Page 26: Backbone js in drupal core

Drupal as webservice

{

"firstName": "John",

"lastName": "Smith",

"age": 25,

"address": {

"streetAddress": "21 2nd Street",

"city": "New York",

"state": "NY",

"postalCode": 10021

}}

Page 27: Backbone js in drupal core

Questions

?

Page 28: Backbone js in drupal core

Materials

● Backbone fundamentals by Andy Osmani● Choosing right js framework with ToDo MVc● Backbone tutorials

Page 29: Backbone js in drupal core

Summary

Page 30: Backbone js in drupal core

Contact

● @MarcinWosinek● presentation notes:http://bit.ly/DcWrocBackbone● [email protected]

Page 31: Backbone js in drupal core

Credits● logo of BackboneJs: https://github.com/documentcloud/backbone/blob/master/docs/images/backbone.png● logo of AngularJs:https://github.com/angular/angular.js/blob/master/images/logo/AngularJS.exports/AngularJS-large.png● http://www.teaching-materials.org/jsmvc/#/2● http://www.teaching-materials.org/jsmvc/#/3● http://www.teaching-materials.org/jsmvc/#/4● http://www.teaching-materials.org/jsmvc/#/18● photo with audience http://www.flickr.com/photos/dougbelshaw/5604047370/in/photostream/ ● kid in sandbox: http://www.flickr.com/photos/thenickster/266142840/● underscore logo: https://github.com/documentcloud/underscore/blob/master/docs/images/underscore.png● http://yeoman.io/