Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8165e2d

Browse files
author
Ben McCormick
committedOct 20, 2015
Add localProps to allow for true instance properties and ES6 classes
1 parent fee53ab commit 8165e2d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
 

‎backbone.js

+26
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394
var Model = Backbone.Model = function(attributes, options) {
395395
var attrs = attributes || {};
396396
options || (options = {});
397+
_.extend(this, _.result(this, 'localProps'));
397398
this.cid = _.uniqueId(this.cidPrefix);
398399
this.attributes = {};
399400
if (options.collection) this.collection = options.collection;
@@ -421,6 +422,11 @@
421422
// You may want to override this if you're experiencing name clashes with model ids.
422423
cidPrefix: 'c',
423424

425+
// localProps is an empty function by default. You can override it with a function
426+
// or object. The results of evaluating localProps will be mixed in to each instance
427+
// of the Model
428+
localProps: function(){},
429+
424430
// Initialize is an empty function by default. Override it with your own
425431
// initialization logic.
426432
initialize: function(){},
@@ -753,6 +759,7 @@
753759
// its models in sort order, as they're added and removed.
754760
var Collection = Backbone.Collection = function(models, options) {
755761
options || (options = {});
762+
_.extend(this, _.result(this, 'localProps'));
756763
if (options.model) this.model = options.model;
757764
if (options.comparator !== void 0) this.comparator = options.comparator;
758765
this._reset();
@@ -781,6 +788,12 @@
781788
// This should be overridden in most cases.
782789
model: Model,
783790

791+
792+
// localProps is an empty function by default. You can override it with a function
793+
// or object. The results of evaluating localProps will be mixed in to each instance
794+
// of the Collection
795+
localProps: function(){},
796+
784797
// Initialize is an empty function by default. Override it with your own
785798
// initialization logic.
786799
initialize: function(){},
@@ -1192,7 +1205,9 @@
11921205
// Creating a Backbone.View creates its initial element outside of the DOM,
11931206
// if an existing element is not provided...
11941207
var View = Backbone.View = function(options) {
1208+
11951209
this.cid = _.uniqueId('view');
1210+
_.extend(this, _.result(this, 'localProps'));
11961211
_.extend(this, _.pick(options, viewOptions));
11971212
this._ensureElement();
11981213
this.initialize.apply(this, arguments);
@@ -1216,6 +1231,11 @@
12161231
return this.$el.find(selector);
12171232
},
12181233

1234+
// localProps is an empty function by default. You can override it with a function
1235+
// or object. The results of evaluating localProps will be mixed in to each instance
1236+
// of the View
1237+
localProps: function(){},
1238+
12191239
// Initialize is an empty function by default. Override it with your own
12201240
// initialization logic.
12211241
initialize: function(){},
@@ -1441,6 +1461,7 @@
14411461
// matched. Creating a new one sets its `routes` hash, if not set statically.
14421462
var Router = Backbone.Router = function(options) {
14431463
options || (options = {});
1464+
_.extend(this, _.result(this, 'localProps'));
14441465
if (options.routes) this.routes = options.routes;
14451466
this._bindRoutes();
14461467
this.initialize.apply(this, arguments);
@@ -1456,6 +1477,11 @@
14561477
// Set up all inheritable **Backbone.Router** properties and methods.
14571478
_.extend(Router.prototype, Events, {
14581479

1480+
// localProps is an empty function by default. You can override it with a function
1481+
// or object. The results of evaluating localProps will be mixed in to each instance
1482+
// of the Router
1483+
localProps: function(){},
1484+
14591485
// Initialize is an empty function by default. Override it with your own
14601486
// initialization logic.
14611487
initialize: function(){},

0 commit comments

Comments
 (0)
Failed to load comments.