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-nest

v0.0.4

Published

Nested data (model attributes) for backbone.js

Downloads

7

Readme

Nest

Nest is a Backbone.js utility that allows for nested data (a/k/a nested module attributes) that can be accessed by model.someNestedCollection. It also provides a smart mechanism for listening to events on nested collections, and will continue to listen to nested data even if the underlying collection changes.

Installation

Nest has been designed to require()'d by browserify, and is currently only supported in that environment. To install:

npm install backbone-nest --save

Code

CI

Nest continuous integrations is handled by Wercker:

wercker status

Testing

Nest maintains 100% test coverage. To manually run the tests, install with with --dev (as above) and run:

gulp testc

You can generate a HTML code coverage report by appending the --html switch

Issues

Issues can be opened in the usual location, pull requests welcome!

Usage

Intializing

Using Nest requires it to be included anywhere in the application:

require('backbone-nest')

Nest uses a simple paradigm to determine which data should be nested. When a model is passed data, it will instinctively remove a key named nested. It will them loop through all of nested's properties, and if it finds a matching nested collection, it will set() it with that data.

JSON Objects

To illustrate, here is a sample JSON object:

{
    foo: 'bar',
    id: 2,
    nested: {
        capitals: [
            {usa: 'dc'},
            {uk: 'london'}
        ]
    }
}

In this case, the model will have an attribute of foo, as well as a nested collection capitals, which will, itself, have two models. The capitals collection can be accessed simply like:

model.capitals

capitals can be accessed fluently like any standard Backbone.Collection (which is what it is). So to access the models:

model.capitals.at(0)

Models

Collections should be setup with the model property. Said model should set nested data in the initialize() method. The model and nested collection would look like:

var nestedCollection = Backbone.Collection.extend({/* ... */})

var Model = Backbone.Model.extend({
    initialize: function(){
        this.nest('nestedColleciton', new nestedCollection())    
    }
})

And the collection:

var someColleciton = Backbone.Collection.extend({
    model: Model
})

This leads to a setup of Collection -> Model -> Collection (and so on if desired)

Nested events

Events will bubble up to the parent collection. Event are listened to on the heights possible element, and hence even if the nested collection is changed/updated/reset the listener stays instant. Here is how to listen. Notice how events are namespaces with the name of the nested collection:

var Model = Backbone.Model.extend({
    initialize: function(){
        this.nest('nestedColleciton', new nestedCollection())    

        // listen for adds
        this.on('nestedColleciton.add', function(){})
        
        // listen for change
        this.on('nestedColleciton.change', function(){})
    }
})
var someColleciton = Backbone.Collection.extend({
    model: Model,
    initialize: function(){
        this.on('nestedColleciton.add', function(){})
    }
})