• This repository has been archived on 08/Jun/2019
  • Stars
    star
    227
  • Rank 169,506 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

lumbar support

Backbone Support Build Status Gem Version

Summary

Backbone Support provides a collection of utility classes for use with Backbone.js. There's no built-in garbage collection for Backbone’s event bindings, and forgetting to unbind can cause bugs and memory leaks. Backbone Support has you covered.

Backbone Support provides two utility classes, SwappingRouter and CompositeView, which are extensions of Backbone's standard View and Router objects. They provide methods that simplify creating and managing nested views and automate the cleanup of events when switching between views.

Inspired by our projects and the Backbone.js on Rails book.

The book contains complete instructions and in-depth coverage of the internals of CompositeView and SwappingRouter, and an example application that shows their usage.

SwappingRouter

A Router subclass the provides a standard way to swap one view for another.

This introduces a convention that all views have a leave() function, responsible for unbinding and cleaning up the view. And the convention that all actions underneath the same Router share the same root element, and define it as el on the router. Additionally, the render method for every view must return that view (a fairly standard backbone convention).

Now, a SwappingRouter can take advantage of the leave() function, and clean up any existing views before swapping to a new one. It swaps into a new view by rendering that view into its own el:

swap: function(newView) {
  if (this.currentView && this.currentView.leave) {
    this.currentView.leave();
  }

  this.currentView = newView;
  $(this.el).empty().append(this.currentView.render().el);
}

An example SwappingRouter would look as follows:

Trajectory.Routers.Stories = Support.SwappingRouter.extend({
  initialize: function(options) {
    this.el = $("div.primary_content");
  },
  routes: {
    "stories": "index",
    "stories/new": "newStory"
  }
  index: function() {
    var view = new Trajectory.Views.StoriesIndex();
    this.swap(view);
  },
  newStory: function() {
    var view = new Trajectory.Views.StoryNew({ model: new Story() });
    this.swap(view);
  }
}

CompositeView

CompositeView provides a convention for a parent view that has one or more child views.

This introduces a convention that all views have a leave() function, responsible for unbinding and cleaning up the view.

CompositeView provides methods for adding and removing children from the parent view.

CompositeView maintains an array of its immediate children as this.children. Using this reference, a parent view's leave() method will invoke leave() on all its children, ensuring that an entire tree of composed views is cleaned up properly.

For child views that can dismiss themselves, such as dialog boxes, children maintain a back-reference at this.parent. This is used to reach up and call this.parent.removeChild(this) for these self-dismissing views.

Compatibility

Backbone Support is compatible with Backbone v0.5.3 and higher, including Backbone v1.0 (as of Backbone Support v0.4.0).

Dependencies

Backbone Support requires the following libraries:

  • jQuery or Zepto
  • Underscore
  • Backbone

Included Versions

For convenience, Backbone Support comes with a vendored copy of Backbone and Underscore to get you up and running as quickly as possible. If you want to use these included files, simply follow the instructions below in the Installation section. The current vendored versions are:

  • Backbone v1.0.0
  • Underscore v1.4.4

Alternate Versions

If you require a different version of Backbone or Underscore than those provided by this gem, simply put the script files in vendor/assets/javascripts which has higher precedence than the versions provided by the gem thanks to the ordering of the search paths in the asset pipeline.

Installation

We recommend you use Backbone-Support with Rails 3.1 or higher in order to take advantage of the asset pipeline.

With Rails 3.1 or higher

Add the gem to your Gemfile:

gem "backbone-support"

And then bundle install.

Before including any part of Backbone Support, you must include its dependencies. In your application.js (or any other manifest file), add the following to load in the needed dependencies:

// jQuery is provided by the 'jquery-rails' gem, included with Rails
//= require jquery
//
// Backbone Support includes Backbone and Underscore, or you can provides
// alternate versions by placing them in vendor/assets/javascripts/
//= require backbone
//= require underscore

To require all of Backbone Support, add the following:

//= require backbone-support

This should be above any usage of SwappingController or SwappingRouter, but below the inclusion of Backbone.js, Underscore, and jQuery.

If you do not wish to include all of backbone-support, you can include individual pieces. Require the support file and the individual assets you wish to use:

//= require backbone-support/support
//= require backbone-support/swapping_router

or:

//= require backbone-support/support
//= require backbone-support/observer
//= require backbone-support/composite_view

With Jammit

First off: rails plugin install [email protected]:thoughtbot/backbone-support.git

In your config/application.rb:

config.middleware.use Rack::Static,
                      :urls => ['/vendor/plugins/backbone-support/lib/assets/javascripts']

And in your config/assets.yml:

javascripts:
  common:
    - public/javascripts/vendor/underscore.js
    - public/javascripts/vendor/backbone.js
    - vendor/plugins/backbone-support/lib/assets/**/*.js

If you have evergreen tests, you will need to change your require statements since these files live in vendor/. Change config/evergreen.rb to be this:

Evergreen.configure do |config|
  config.public_dir = '.'
end

Your individual specs will then need the full root path in require. For example:

requirePublic = function(path) {
  require("/public/javascripts/" + path);
};

requirePublic("vendor/underscore.js");
requirePublic("vendor/backbone.js");

require("/vendor/plugins/backbone-support/lib/assets/javascripts/backbone-support.js");
require("/vendor/plugins/backbone-support/lib/assets/javascripts/backbone-support/composite_view.js");
require("/vendor/plugins/backbone-support/lib/assets/javascripts/backbone-support/swapping_router.js");

Development

First:

bundle

While TDD'ing:

bundle exec rake jasmine

To not open tests in a browser window:

bundle exec rake jasmine:ci

License

Copyright 2012 thoughtbot. Please check LICENSE for more details.

More Repositories

1

guides

A guide for programming in style.
Ruby
9,327
star
2

bourbon

A Lightweight Sass Tool Set
Ruby
9,100
star
3

paperclip

Easy file attachment management for ActiveRecord
Ruby
9,055
star
4

laptop

A shell script to set up a macOS laptop for web and mobile development.
Shell
8,416
star
5

dotfiles

A set of vim, zsh, git, and tmux configuration files.
Shell
7,864
star
6

factory_bot

A library for setting up Ruby objects as test data.
Ruby
7,826
star
7

administrate

A Rails engine that helps you put together a super-flexible admin dashboard.
JavaScript
5,797
star
8

neat

A fluid and flexible grid Sass framework
Ruby
4,444
star
9

suspenders

A Rails template with our standard defaults, ready to deploy to Heroku.
Ruby
3,922
star
10

til

Today I Learned
3,903
star
11

clearance

Rails authentication with email & password.
Ruby
3,629
star
12

Argo

Functional JSON parsing library for Swift
Swift
3,495
star
13

shoulda-matchers

Simple one-liner tests for common Rails functionality
Ruby
3,469
star
14

high_voltage

Easily include static pages in your Rails app.
Ruby
3,141
star
15

rcm

rc file (dotfile) management
Perl
2,990
star
16

factory_bot_rails

Factory Bot β™₯ Rails
Ruby
2,972
star
17

shoulda

Makes tests easy on the fingers and the eyes
Ruby
2,184
star
18

expandable-recycler-view

Custom Android RecyclerViewAdapters that collapse and expand
Java
2,073
star
19

capybara-webkit

A Capybara driver for headless WebKit to test JavaScript web apps
Ruby
1,976
star
20

gitsh

An interactive shell for git
Ruby
1,957
star
21

Tropos

Weather and Forecasts for Humans
Swift
1,518
star
22

refills

[no longer maintained]
CSS
1,513
star
23

design-sprint

Product Design Sprint Material
1,415
star
24

bitters

Add a dash of pre-defined style to your Bourbon.
HTML
1,398
star
25

griddler

Simplify receiving email in Rails
Ruby
1,375
star
26

trail-map

Trails to help designers and developers learn various topics.
1,219
star
27

appraisal

A Ruby library for testing your library against different versions of dependencies.
Ruby
1,194
star
28

hotwire-example-template

A collection of branches that transmit HTML over the wire.
Ruby
989
star
29

parity

Shell commands for development, staging, and production parity for Heroku apps
Ruby
882
star
30

Runes

Infix operators for monadic functions in Swift
Swift
829
star
31

cocaine

A small library for doing (command) lines.
Ruby
788
star
32

fishery

A library for setting up JavaScript objects as test data
TypeScript
759
star
33

flutie

View helpers for Rails applications
Ruby
730
star
34

TBAnnotationClustering

Example App: How To Efficiently Display Large Amounts of Data on iOS Maps
Objective-C
728
star
35

ember-cli-rails

Unify your EmberCLI and Rails Workflows
Ruby
714
star
36

vim-rspec

Run Rspec specs from Vim
Vim Script
650
star
37

climate_control

Modify your ENV
Ruby
512
star
38

constable

Better company announcements
Elixir
511
star
39

carnival

An unobtrusive, developer-friendly way to add comments
Haskell
501
star
40

ruby-science

The reference for writing fantastic Rails applications
Ruby
494
star
41

Curry

Swift implementations for function currying
Swift
493
star
42

pacecar

Generated scopes for ActiveRecord classes
Ruby
437
star
43

hoptoad_notifier

Reports exceptions to Hoptoad
Ruby
408
star
44

fake_stripe

A Stripe fake so that you can avoid hitting Stripe servers in tests.
Ruby
393
star
45

json_matchers

Validate your JSON APIs
Ruby
381
star
46

Swish

Nothing but Net(working)
Swift
364
star
47

paul_revere

A library for "one off" announcements in Rails apps.
Ruby
298
star
48

stencil

Android library, written exclusively in kotlin, for animating the path created from text
Kotlin
282
star
49

Perform

Easy dependency injection for storyboard segues
Swift
280
star
50

superglue

A productive library for Classic Rails, React and Redux
JavaScript
275
star
51

upcase

Sharpen your programming skills.
Ruby
275
star
52

testing-rails

Source code for the Testing Rails book
HTML
269
star
53

proteus

[no longer maintained]
Ruby
254
star
54

Delta

Managing state is hard. Delta aims to make it simple.
Swift
246
star
55

foundry

Providing a new generation of vector assets and infinite possibility for the interactive web and mobile applications
CSS
233
star
56

limerick_rake

A collection of useful rake tasks.
Ruby
232
star
57

shoulda-context

Shoulda Context makes it easy to write understandable and maintainable tests under Minitest and Test::Unit within Rails projects or plain Ruby projects.
Ruby
219
star
58

terrapin

Run shell commands safely, even with user-supplied values
Ruby
216
star
59

Superb

Pluggable HTTP authentication for Swift.
Swift
203
star
60

jack_up

[DEPRECATED] Easy AJAX file uploading in Rails
Ruby
202
star
61

fistface

DIY @font-face web service.
Ruby
182
star
62

squirrel

Natural-looking Finder Queries for ActiveRecord
Ruby
178
star
63

sortable_table

Sort HTML tables in your Rails app.
Ruby
157
star
64

write-yourself-a-roguelike

Write Yourself A Roguelike: Ruby Edition
Ruby
155
star
65

pester

Automatically ask for a PR review
Ruby
147
star
66

jester

REST in Javascript
JavaScript
146
star
67

complexity

A command line tool to identify complex code
Rust
142
star
68

kumade

Heroku deploy tasks with test coverage (DEPRECATED, NO LONGER BEING DEVELOPED)
Ruby
137
star
69

proteus-middleman

[no longer maintained]
CSS
133
star
70

FunctionalJSON-swift

Swift
133
star
71

capybara_discoball

Spin up an external server just for Capybara
Ruby
128
star
72

tropos-android

Weather and Forecasts for Humans
Kotlin
128
star
73

ModalPresentationView

Remove the boilerplate of modal presentations in SwiftUI
Swift
125
star
74

react-native-typescript-styles-example

A template react native project for ergonomic styling structure and patterns.
TypeScript
123
star
75

vimulator

A JavaScript Vim simulator for demonstrations
JavaScript
119
star
76

bourne

[DEPRECATED] Adds test spies to mocha.
Ruby
114
star
77

formulator

A form library for Phoenix
Elixir
106
star
78

poppins

Gifs!
Objective-C
106
star
79

tailwindcss-aria-attributes

TailwindCSS variants for aria-* attributes
JavaScript
100
star
80

ghost-theme-template

A project scaffold for building ghost themes using gulp, node-sass, & autoprefixer
HTML
91
star
81

paperclip_demo

Paperclip demo application
Ruby
87
star
82

middleman-template

The base Middleman application used at thoughtbot, ready to deploy to Netlify.
CSS
86
star
83

proteus-jekyll

[no longer maintained]
CSS
84
star
84

report_card

metrics and CI are for A students.
Ruby
77
star
85

ios-sample-blender

Sample code for the Blending Modes blog post
Objective-C
76
star
86

yuri-ita

Create powerful interfaces for filtering, searching, and sorting collections of items.
Ruby
76
star
87

baccano

[no longer maintained]
HTML
74
star
88

goal-oriented-git

A practical book about using Git
HTML
73
star
89

ios-on-rails

A guide to building a Rails API and iOS app
HTML
72
star
90

art_vandelay

Art Vandelay is an importer/exporter for Rails 6.0 and higher.
Ruby
71
star
91

maybe_haskell

Programming without Null
HTML
71
star
92

redbird

A Redis adapter for Plug.Session
Elixir
67
star
93

maintaining-open-source-projects

A successful open source project is not only one that is original, solves a particular problem well, or has pristine code quality. Those are but the tip of the iceberg, which we'll thoroughly dissect with this book.
Shell
67
star
94

templates

Documentation templates for open source projects.
64
star
95

FOMObot

A slack bot to help with FOMO.
Haskell
61
star
96

BotKit

BotKit is a Cocoa Touch static library for use in iOS projects. It includes a number of helpful classes and categories that are useful during the development of an iOS application.
Objective-C
61
star
97

react-native-template

Template React Native project to be used with Cookiecutter
JavaScript
60
star
98

CombineViewModel

An implementation of the Model-View-ViewModel (MVVM) pattern using Combine.
Swift
59
star
99

flightdeck

Terraform modules for rapidly building production-grade Kubernetes clusters following SRE practices
HCL
55
star
100

design-for-developers-starter-kit

A starter project for design for developer students
CSS
54
star