npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone.computed

v0.0.6

Published

Simple computed fields for Backbone models

Downloads

6

Readme

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.