0.7.5 • Published 7 years ago

backbone_views v0.7.5

Weekly downloads
5
License
BSD-2-Clause
Repository
github
Last release
7 years ago

Backbone views

Provides some basic views to help you get started using Backbone

All examples below are written in coffeescript. (That's what this library is written in.)

Views

MixinView

Provides an easy interface to mixins. All class based views in this package will use this as their base class.

bv = require("backbone_views")

class MyView extends bv.views.MixinView
  mixins: [bv.mixins.SelectorMixin]

A mixin is just a class. The prototype will be mixed in the current class. If a mixin method is provided by the mixin, the it will be called during the view's initialize method.

This view also provides basic rendering. It will call the template property of the view if it exists.

Default Generic Views

The views below are just simple implementations that use the mixins listed at the bottom of the doc. For complicated stuff, you probably should write your own.

ListView

Uses the ListMixin below and a DetailView to provide a functioning list. This view only requires that you give it a collection and it will make an auto managed list.

CreateView

A simple view that uses the FormMixin to create and add a model to a collection. You must provide a collection for this view to work.

DetailView

Uses the DetailMixin below

UpdateView

A simple view that updates a model using the FormMixin

DeleteView

A simple view that provides a confirm form before destroying a model

Mixins

NunjucksMixin

Gives function to render a nunjucks form.

bv = require("backbone_views")


class MyView extends bv.views.MixinView
  mixins: [bv.mixins.NunjucksMixin]
  template: require("my_view.html")

You can provide a getContext function to extend the context

SelectorMixin

Selects elements after rendering

bv = require("backbone_views")


class MyView extends bv.views.MixinView
  ui:
    input: ".form input#foo"

  # here is how you can use it, it will be jQueryied
  myFunc: () -> alert @ui.input.val()

FormMixin

Provides a renderForm method to render a form from the npm forms package. (Dang, that sentence is not formed well :D )

bv = require "backbone_views"
forms = require "forms"


class MyView extends bv.views.MixinView
  mixins: [bv.mixins.FormMixin]  
  form: forms.create(
    name: form.fields.string(required: true)
  )

BootstrapFormMixin

A form mixin that provides a bootstrapping form render function bootstrapField for those who use bootstrap3

ListMixin

When this mixin is provided a collection it will register listeners to auto populate the collection in the view.

If a listSelector isn't provided or selects nothing, it will assume the list should be populated on the view's el.

If an emptySelector is provided, it will hide/show the selector's results whenever the list is empty/exists

bv = require("backbone_views")
Backbone = require("backbone")


class MyView extends bv.views.MixinView
  mixins: [bv.mixins.ListMixin]  
  listSelector: ".list"
  emptySelector: ".empty"
  collection: new Backbone.Collection([{foo: "boo"}])
  template: '...'

DetailMixin

This provides an easy way to bind properties to a view. Set the property name and a selector on the bindings property and then this model and the dom will be kept in sync

By default it will autobind unbound attrs to "data-{{ name }}", but you can set the autoBind property to change it to something else or turn off this feature.

You can further customize what bound elements will do by setting the "data-{{ name }}" value:

  • toggle: display or hide depending on data value
  • inverse-toggle: like toggle but inverted
  • pluralize: shows element contents if value not 1
  • data: sets a "data-" attribute instead of content
  • href: sets the href instead of content
  • attr: sets an attr instead of content
  • prop: sets a property instead of content (booleans)
  • prop-inverse: like prop but inverted

by default it will replace the contents of the element

bv = require("backbone_views")
Backbone = require("backbone")


class MyView extends bv.views.MixinView
  mixins: [bv.mixins.DetailView]  
  bindings:
    fruit: "div .apple"
  template: '...'
0.7.5

7 years ago

0.7.4

7 years ago

0.7.3

7 years ago

0.7.2

7 years ago

0.7.1

7 years ago

0.7.0

8 years ago

0.6.19

8 years ago

0.6.18

8 years ago

0.6.17

8 years ago

0.6.16

8 years ago

0.6.15

8 years ago

0.6.14

8 years ago

0.6.13

8 years ago

0.6.12

8 years ago

0.6.11

8 years ago

0.6.10

8 years ago

0.6.9

8 years ago

0.6.8

8 years ago

0.6.7

8 years ago

0.6.6

8 years ago

0.6.5

8 years ago

0.6.3

8 years ago

0.6.2

8 years ago

0.6.1

8 years ago

0.5.20

8 years ago

0.5.19

8 years ago

0.5.18

8 years ago

0.5.17

8 years ago

0.5.16

8 years ago

0.5.15

8 years ago

0.5.14

8 years ago

0.5.13

8 years ago

0.5.12

8 years ago

0.5.0

9 years ago

0.4.7

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago