documentcloud/backbone — pull request #1086 by hokaccha (2012-03-09 17:07:57+00:00)
Title: Add `model.normalize`

`model.normaize` can normaize attributes before each `set()`.

For example.

var Model = Backbone.Model.extend({
normalize: function(attrs) {
attrs.num = parseInt(attrs.num);
attrs.dateObj = new Date(dateStr);
}
});

var model = new Model({
num: '10',
dateStr: '2012-03-01'
});

model.get('num') === 10; // not string
model.get('dateObj') instanceof Date;

model.set('num', '20');
model.get('num') === 20;