Backbone Dotattr

A utility for accessing deep attributes in Backbone.js models with dot-syntax notation.


I’ve created a little utility for accessing deep attributes in Backbone.js models with dot-syntax.

var song = new Backbone.Model({
    title: "Lucy In The Sky With Diamonds",
    album: new Backbone.Model({
        title: "Sgt. Pepper's Lonely Hearts Club Band",
        release: {
            year: "1987"
        }
    })
});

// Previously you had to use a slightly more verbose approach
// and had to remember which attributes were objects or models.
song.get('album').get('release').year

// Using dot-syntax
// Deep model attributes
song.get('album.title'); // "Sgt. Pepper's Lonely Hearts Club Band"

// Deep object attributes
song.get('album.release.year'); // "1987"

// Regular attributes
song.get('title') // "Lucy In The Sky With Diamonds"

GitHub stars

← All posts