Permalink
Browse files

Adding empty initialize() function for all Backbone classes, to make …

…calling super() a easier to get right.
  • Loading branch information...
1 parent 7e363df commit 99fbc0822434e284890bc729b19666c3a715dd61 @jashkenas committed Nov 16, 2010
Showing with 20 additions and 4 deletions.
  1. +20 −4 backbone.js
View
@@ -118,7 +118,7 @@
this.set(attributes || {}, {silent : true});
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
- if (this.initialize) this.initialize(attributes, options);
+ this.initialize(attributes, options);
};
// Attach all inheritable methods to the Model prototype.
@@ -131,6 +131,10 @@
// Has the item been changed since the last `"change"` event?
_changed : false,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// Return a copy of the model's `attributes` object.
toJSON : function() {
return _.clone(this.attributes);
@@ -368,7 +372,7 @@
this._boundOnModelEvent = _.bind(this._onModelEvent, this);
this._reset();
if (models) this.refresh(models, {silent: true});
- if (this.initialize) this.initialize(models, options);
+ this.initialize(models, options);
};
// Define the Collection's inheritable methods.
@@ -378,6 +382,10 @@
// This should be overridden in most cases.
model : Backbone.Model,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// The JSON representation of a Collection is an array of the
// models' attributes.
toJSON : function() {
@@ -575,7 +583,7 @@
options || (options = {});
if (options.routes) this.routes = options.routes;
this._bindRoutes();
- if (this.initialize) this.initialize(options);
+ this.initialize(options);
};
// Cached regular expressions for matching named param parts and splatted
@@ -586,6 +594,10 @@
// Set up all inheritable **Backbone.Controller** properties and methods.
_.extend(Backbone.Controller.prototype, Backbone.Events, {
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// Manually bind a single named route to a callback. For example:
//
// this.route('search/:query/p:num', 'search', function(query, num) {
@@ -733,7 +745,7 @@
this._configure(options || {});
this._ensureElement();
this.delegateEvents();
- if (this.initialize) this.initialize(options);
+ this.initialize(options);
};
// jQuery lookup, scoped to DOM elements within the current view.
@@ -756,6 +768,10 @@
$ : jQueryDelegate,
jQuery : jQueryDelegate,
+ // Initialize is an empty function by default. Override it with your own
+ // initialization logic.
+ initialize : function(){},
+
// **render** is the core function that your view should override, in order
// to populate its element (`this.el`), with the appropriate HTML. The
// convention is for **render** to always return `this`.

0 comments on commit 99fbc08

Please sign in to comment.