Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c355922416
Fetching contributors…

Cannot retrieve contributors at this time

98 lines (70 sloc) 2.101 kb

Backbone.getters.setters

A custom getters and setters plugin for Backbone.js.

Getting started

Include Backbone (including underscore.js) in your page before including the Backbone.getters.setters plugin and you're all set to go.

The plugin is tested with Backbone version 0.9.1

Configure getters on the Model

Your model should extend Backbone.GSModel instead of Backbone.Model in order to support getters:

var MyModel = Backbone.GSModel.extend({
});

Configure your getters by adding a getter function for each attribute:

var MyModel = Backbone.GSModel.extend({
  getters: {
        fullName: function() {
            return this.get('firstName') + ' ' + this.get('lastName');
        }
  },

  defaults: {
    firstName: 'Lady',
    lastName: 'Gaga'
  }
});

Then simply call the regular get method:

var someModel = new MyModel();
alert(someModel.get('fullName'));

The output of the above will be an alert with the text: 'Lady Gaga'.

Configure setters on the Model

Your model should extend Backbone.GSModel instead of Backbone.Model in order to support setters:

var MyModel = Backbone.GSModel.extend({
});

Configure your setters by adding a setter function for each attribute:

var MyModel = Backbone.GSModel.extend({
  setters: {
        firstName: function(value) {
            return value.toUpperCase();
        },
      lastName:: function(value) {
        return value.toLowerCase();
        }
  },

  defaults: {
    firstName: 'Lady',
    lastName: 'Gaga'
  }
});

In the above example, the setters were already called by the defaults hash.

The value of 'firstName' is now 'LADY', the value of 'lastName' is now 'gaga'.

You can also call the set method as usual:

someModel.set('firstName', 'letters');

And now the value of 'firstName' is 'LETTERS'.

You can also set multiple attributes as regular:

someModel.set({
  'firstName': 'everything',
  'lastName': 'NUMBERS'
});

And now the value of 'firstName' is 'EVERYTHING' and the value of 'lastName' is 'numbers'.

ENJOY!

Jump to Line
Something went wrong with that request. Please try again.