Slide 1

Slide 1 text

How to develop Backbone plugins …for the greater good!

Slide 2

Slide 2 text

Dimitris Tsironis Front-End Engineer at Splunk Inc. github/tsironis & twitter/tsironakos Who

Slide 3

Slide 3 text

• Library that provides devs with isolated abstractions • No magic at all • 100% un-opinionated code • A lot of boilerplate, as well • all of which could lead to… Backbone.js or Barebone.js™?

Slide 4

Slide 4 text

PROBLEMS

Slide 5

Slide 5 text

• Modularized plugins that hook to Backbone.js • No boilerplate code! • Reusable, good for the environment :) • Ready to use collection of plugins (on Github) Solving this problem

Slide 6

Slide 6 text

• Easy extensibility • Small source code • Speed and efficiency • Limited API But Backbone.js also offers…

Slide 7

Slide 7 text

• Extending Backbone’s prototype functions • Custom events (namespaced, eg. search:done) • Descriptive API functions Tips and Tricks

Slide 8

Slide 8 text

• Identify the problem(s) you’re trying to solve • Abstract the shit out of them • Solve for (x), not for specific applications • Share singletons across pages / modules • Take in mind extendability and future changes • Give API for everything contained inside, people (incl. you) will find ingenious ways to use your stuff Let’s get to work!

Slide 9

Slide 9 text

Scenario #1 CollectionView extends Backbone.View in order to reuse this common code over and over again

Slide 10

Slide 10 text

• Render all models • Handle add, reset, remove events • Use compiled template (Underscore’s memoize function is our friend) • Associate children views with models • Efficient DOM handling Features (batteries incl.)

Slide 11

Slide 11 text

Scenario #2 Create a Backbone.Model which has immutable attributes

Slide 12

Slide 12 text

Backbone.Immutable = (function (Backbone, _) { var attrs = {}; var Immutable = Backbone.Model.extend({ ... }); return Immutable; })(Backbone, _);

Slide 13

Slide 13 text

... constructor: function (attributes, options) { Backbone.Model.prototype.constructor.call(this, arguments); // Handle default attributes on instantiation attrs = attributes; }, get: function(key) { return attrs[key]; }, ...

Slide 14

Slide 14 text

... set: function (key, val) { var data; if (key == null) return this; // Handle both `key, value` and `{key: value}` style arguments. if (typeof key === 'object') { data = key; } else { (data = {})[key] = val; } _.each(data, function(value, key) { if (_.isUndefined(attrs[key])) { attrs[key] = value; } else { console.warn("You're not supposed to change this value."); } }); }, toJSON: function() { return _.clone(attrs); } ...

Slide 15

Slide 15 text

• http://backboneplugins.com/ (Derick Bailey’s book) • Backbone annotated source (not docs!) - http://backbonejs.org/ docs/backbone.html • Backbone.Marionette source code - https://github.com/ marionettejs/backbone.marionette • https://github.com/tsironis/backbone.immutable • https://github.com/jmorrell/backbone.obscura Resources

Slide 16

Slide 16 text

No content