Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to Develop Backbone Plugins (...for the gre...

How to Develop Backbone Plugins (...for the greater good!)

Dimitris Tsironis

April 09, 2015
Tweet

More Decks by Dimitris Tsironis

Other Decks in Technology

Transcript

  1. • 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™?
  2. • 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
  3. • Easy extensibility • Small source code • Speed and

    efficiency • Limited API But Backbone.js also offers…
  4. • Extending Backbone’s prototype functions • Custom events (namespaced, eg.

    search:done) • Descriptive API functions Tips and Tricks
  5. • 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!
  6. • 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.)
  7. Backbone.Immutable = (function (Backbone, _) { var attrs = {};

    var Immutable = Backbone.Model.extend({ ... }); return Immutable; })(Backbone, _);
  8. ... constructor: function (attributes, options) { Backbone.Model.prototype.constructor.call(this, arguments); // Handle

    default attributes on instantiation attrs = attributes; }, get: function(key) { return attrs[key]; }, ...
  9. ... 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); } ...
  10. • 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