Skip to content
This repository has been archived by the owner on Jan 6, 2019. It is now read-only.

meleyal/backbone.widget

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backbone.Widget

Create Backbone.Views via jQuery.

Backbone.Widget extends Backbone.View with some simple helpers for creating jQuery widgets/plugins ($.fn).

This enables creating Backbone.View instances with jQuery syntax:

$('.example').myView([options]);

Why?

At the code level Backbone.View offers a nice way to structure your jQuery widget/plugin code. Backbone.Widget just provides some minimal glue code to make this easy.

At the application / architecture level, the JS Widgets approach can offer a hybrid / DOM-centric alternative to single page apps; with static HTML from the server providing the foundation for multiple mini-applications (widgets).

Usage

JavaScript

// extend Backbone.Widget
MyView = Backbone.Widget.extend({
  initialize: function(options) {
    console.log(this.el, options);
  }
});

// export as widget
MyView.exportWidget('myView');

// call on element
$('.example').myView([options]);

CoffeeScript

# extend Backbone.Widget
class MyView extends Backbone.Widget

  # export as widget
  @exportWidget 'myView'

  initialize: (options) ->
    console.log @el, options

# call on element
$('.example').myView([options])

Tricks

Calling view methods

View methods can be called directly on the element

$('.example').myView('remove')

View instance

A reference to the view instance is stored in the element's $.data.

view = $('.example').data('myView');
console.log(view.cid); // view0

Namespace

The namespace option is passed to the view instance for convenience. This can be useful for e.g. namespacing events.

...
initialize: function(options) {
  console.log(options.namespace); // myView
}
...

Auto remove

Widgets's can optionally remove themselves on a given event. This can be useful with pjax / turbolinks where you need to routinely cleanup views between "page loads". See the wiki page for more details.

About

Create Backbone.Views via jQuery

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published