GithubHelp home page GithubHelp logo

therealjeffg / backbone-bind-to Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rstankov/backbone-bind-to

1.0 0.0 0.0 216 KB

Backbone.js extension for automatic binding and unbinding of model events to views.

License: Other

backbone-bind-to's Introduction

Backbone.BindTo

Backbone.js extension for automatic binding and unbinding of model events to views.

Features

BindToModel

In a lot of Backbone.js applications when you want to react to model events you have to write:

window.UserCardView = Backbone.View.extend({
  initialize: function() {
    this.model.bind('change:name',  this.renderName,  this);
    this.model.bind('change:email', this.renderEmail, this);
  },
  remove: function() {
    this.model.unbind('change:name',  this.renderName,  this);
    this.model.unbind('change:email', this.renderEmail, this);
    Backbone.View.prototype.remove.apply(this, arguments);
  },
  renderName:  function() { /* ... code ... */ },
  renderEmail: function() { /* ... code ... */ }
});

With Backbone.BindTo you can just do:

window.UserCardView = Backbone.View.extend({
  bindToModel: {
    'change:name':  'renderName',
    'change:email': 'renderEmail'
  },
  renderName:  function() { /* ... code ... */ },
  renderEmail: function() { /* ... code ... */ }
});

BindToCollection

Of course, there is a similar method for binding to collection events:

window.TodoListView = Backbone.View.extend({
  bindToCollection: {
    'add': 'renderNewTask'
  },
  renderNewTask: function() { /* ... code ... */ }
});

bindTo

You can also use a generic bind function:

window.CommentView = Backbone.View.extend({
  initialize: function() {
    this.parentView.on('editing:start', this.onEditingStart, this);
  },
  remove: function() {
    this.parentView.off('editing:start', this.onEditingStart, this);
    Backbone.View.prototype.remove.apply(this, arguments);
  },
  onEditingStart:  function() { /* ... code ... */ }
});

Can be simplified as:

window.CommentView = Backbone.View.extend({
  initialize: function() {
    this.bindTo(this.parentView, 'editing:start', 'onEditingStart');
  },
  onEditingStart:  function() { /* ... code ... */ }
});

Remove

Backbone.BindTo automatically unbinds from all model and collection events when the view element is removed via Backbone.View#remove. Also unbinds from all events binded via #bindTo.

noConflict

If extending directly Backbone.View bothers you. You can use the Backbone.BindTo.noConflict method. It restores Backbone.View to its original value. And returns the Backbone.BindTo.View object which has the bindToModel and bindToCollection helpers.

window.BindToView = Backbone.BindTo.noConflict()

Installing

Just copy lib/backone_bind_to.js into your project. Or if you are using CoffeeScript you can use directly - src/backbone_bind_to.coffee.

Requirements

Backbone.js - 0.9.2+

Running the tests

Just open - test/runner.html

Contributing

Every fresh idea and contribution will be highly appreciated.

If you are making changes please do so in the coffee files. And then compile them with:

cake build

License

MIT License.

backbone-bind-to's People

Contributors

rstankov avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.