A Backbone.js Playground
A simple playground for learning backbone.js without having to install or configure anything yourself.
HTML
<script src="http://documentcloud.github.com/underscore/underscore.js"></script>
<script src="http://documentcloud.github.com/backbone/backbone.js"></script>
<script src="https://getfirebug.com/firebug-lite.js"></script>
JavaScript
var MyModel = Backbone.Model.extend({
defaults: {
theDate: null
},
validate: function (attributes) {
if (!_.isDate(attributes.theDate)) {
return 'theDate must be a date';
}
},
set: function (attributes, options) {
if (_.isNumber(attributes.theDate)) {
attributes.theDate = new Date(attributes.theDate);
}
Backbone.Model.prototype.set.call(this, attributes, options);
}
});
var model = new MyModel({ theDate: (new Date(1, 1, 2011)).getTime() });
console.log(model.get('theDate')); // Date {Sat Aug 04 1906 00:00:00 GMT-0500 (CST)}
var MyModelList = Backbone.Collection.extend({
model: MyModel
});
var list = new MyModelList([{ theDate: (new Date(1, 1, 2011)).getTime() }]);
console.log(list.length); // 0!