GithubHelp home page GithubHelp logo

backbone.computed's Introduction

backbone.computed

Build Status Dependancy Status Dev Dependancy Status

Simple computed fields for Backbone models

Introduction

Ever get the feeling that there is something missing from backbone models? Well I got the feeling especially when using backbone models with so called "logic-less" template systems such as Mustache, Hogan and handlebars.

Although handlebars has some built in "logic" helpers (which you can add to) and Marionette has helpfull features such as templateHelpers some properties are computed over and over again and they really belong on the model.

This is where backbone.computed attempts to bridge the gap.

Usage

Similar to model.events, you define your computed properties to model.computed:

var Person = Backbone.Model.extend({
  computed: {
    fullName: function () {
      return this.get('firstName') + ' ' + this.get('lastName');
    }
  }
});

var person = new Person({
  firstName: 'Tim',
  lastName: 'Burton',
  age: 20
});

you can access your computed properties just like normal model attributes:

person.get('fullName'); // returns 'Tim Burton'

Computed properties do not come into effect when calling save:

person.save();
/**
 *  submitted in ajax body:
 *  {
 *    firstName: 'Tim',
 *    lastName: 'Burton',
 *    age: 20
 *  }
 */

If you are using a handlebars and want to serialise your model ready for your template:

<script id="person-template" type="text/x-handlebars-template">
  <div class="person">
    <h1>{{fullName}}</h1>
    <div class="details">
      Age: {{age}}
    </div>
  </div>
</script>
var source   = $("#person-template").html();
var template = Handlebars.compile(source);
var html     = template(person.toJSON());

results in:

<div class="person">
  <h1>Tim Burton</h1>
  <div class="details">
    Age: 20
  </div>
</div>

If using Marionette in conjunction with backbone.computed things get really simple as when Marionette passes the model data to the views template it just calls toJSON on the backbone model.

The example below is using Marionettes default template system which is really just underscores template system but this can easily be swapped out for handlebars or another:

<script id="person-template" type="text/template">
  <div class="person">
    <h1><%= fullName %></h1>
    <div class="details">
        Age: <%= age %>
    </div>
  </div>
</script>
var PersonView = Marionette.ItemView.extend({
    template: '#person-template'
});
...
var person = new Person({
  firstName: 'Tim',
  lastName: 'Burton',
  age: 20
});
layoutView.content.show(new PersonView({ model: person }));

this will again results in:

<div class="person">
  <h1>Tim Burton</h1>
  <div class="details">
    Age: 20
  </div>
</div>

Duck Punching

This plugin adds its functionality by duck punching the original implementations of get and toJSON on Backbone.Model.

If no computed properties exist the original implementations will kick in.

backbone.computed's People

Contributors

milkshakeuk avatar

Watchers

 avatar

backbone.computed's Issues

Spelling mistake in README.md 'implamentations' x2

This plugin adds its functionality by duck punching the original implamentations of get and toJSON on Backbone.Model.

If no computed properties exist the original implamentations will kick in.

Nice project, looks pretty cool!

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.