backbone-nesty

Support nested data types like collections and models within your Backbone.js models

Backbone Nesty

Support nested data types like collections, arrays, and models within your Backbone.js Models

  1. Install Node.js
  2. npm install --save backbone-nesty
  1. See Browserify
// Import 
var backbone = require('backbone');
var BackboneNestyModel = require('backbone-nesty').BackboneNestyModel;
var models = {};
var collections = {};
 
// Eye Model 
models.Eye = backbone.Model.extend({
    attributes: {
        color: null,
        location: null,  // left or right 
        open: false
    }
});
 
// Eye Collection 
collections.Eye = backbone.Collection.extend({
    model: models.Eye
});
 
// Mouth Model 
var Mouth = Backbone.Model.extend({
    attributes: {
        open: false
    }
});
 
// Head Model 
models.Head = BackboneNestyModel.extend({
    // Define our nested collections 
    collections: {
        eyes: collections.Eye
    },
 
    // Define our nested models 
    models: {
        mouth: models.Mouth
    }
});
 
// Instantiate our head with our nested data 
var myHead = new MyModel({
 
    // will create a mouth model with this data 
    mouth: {
        open: true
    },
    
    // will create an eyes collection with this data 
    eyes: [
        
        // will create an eye model with this data 
        {
            color: 'green',
            location: 'left',
            open: true
        },
        
        // will create an eye model with this data 
        {
            color: 'green',
            location: 'right',
            open: true
        }
    ]
});
 
// Nested Getter 
console.log(myHead.get('eyes.0.open')); // true 
// ^ equiv to myHeader.get('eyes').at(0).get('open') 
 
// Nested Setter 
myHead.set('eyes.0.open', false);
// ^ equiv to myHeader.get('eyes').at(0).set('open', false) 
 
// Check 
console.log(myHead.get('eyes.0.open')); // false 

You can discover the history inside the History.md file

Check out the Backers.md file to discover all the amazing people who financially supported the development of this project.

Licensed under the incredibly permissive MIT License
Copyright © 2013+ Bevry Pty Ltd