From abd64dca83fc6438d94f43a69a303861d39bba1b Mon Sep 17 00:00:00 2001
From: Jeremy Ashkenas
- constructor
var Note = Backbone.Model.extend({
+
+ initialize: function() { ... },
author: function() { ... },
@@ -450,11 +452,12 @@ var Note = Backbone.Model.extend({
new Model([attributes])
+ constructor / initializenew Model([attributes])
When creating an instance of a model, you can pass in the initial values
of the attributes, which will be set on the
- model.
+ model. If you define an initialize function, it will be invoked when
+ the model is created.
@@ -736,15 +739,16 @@ bill.set({name : "Bill Jones"});
- constructornew Collection([models], [options])
+ constructor / initializenew Collection([models], [options])
When creating a Collection, you may choose to pass in the initial array of models.
The collection's comparator function
- may be included as an option.
+ may be included as an option. If you define an initialize function, it will be
+ invoked when the collection is created.
-window.Tabs = new TabSet([tab1, tab2, tab3]); +var tabs = new TabSet([tab1, tab2, tab3]);
@@ -1108,6 +1112,10 @@ var DocumentRow = Backbone.View.extend({ "click .button.edit": "openEditDialog", "click .button.delete": "destroy" }, + + initialize: function() { + _.bindAll(this, "render"); + }, render: function() { ... @@ -1117,13 +1125,15 @@ var DocumentRow = Backbone.View.extend({
- constructornew View([options])
+ constructor / initializenew View([options])
When creating a new View, the options you pass are attached to the view
as this.options, for future reference. There are several special
options that, if passed, will be attached directly to the view:
model, collection,
el, id, className, and tagName.
+ If the view defines an initialize function, it will be called when
+ the view is first created.