Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a preinitialize method to allow for true instance properties and ES6 classes #3827

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@
var Model = Backbone.Model = function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.preinitialize.apply(this, arguments);
this.cid = _.uniqueId(this.cidPrefix);
this.attributes = {};
if (options.collection) this.collection = options.collection;
Expand Down Expand Up @@ -422,6 +423,10 @@
// You may want to override this if you're experiencing name clashes with model ids.
cidPrefix: 'c',

// preinitialize is an empty function by default. You can override it with a function
// or object. preinitialize will run before any instantiation logic is run in the Model.
preinitialize: function(){},

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
Expand Down Expand Up @@ -756,6 +761,7 @@
// its models in sort order, as they're added and removed.
var Collection = Backbone.Collection = function(models, options) {
options || (options = {});
this.preinitialize.apply(this, arguments);
if (options.model) this.model = options.model;
if (options.comparator !== void 0) this.comparator = options.comparator;
this._reset();
Expand Down Expand Up @@ -785,6 +791,11 @@
// This should be overridden in most cases.
model: Model,


// preinitialize is an empty function by default. You can override it with a function
// or object. preinitialize will run before any instantiation logic is run in the Collection.
preinitialize: function(){},

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
Expand Down Expand Up @@ -1221,6 +1232,7 @@
// if an existing element is not provided...
var View = Backbone.View = function(options) {
this.cid = _.uniqueId('view');
this.preinitialize.apply(this, arguments);
_.extend(this, _.pick(options, viewOptions));
this._ensureElement();
this.initialize.apply(this, arguments);
Expand All @@ -1244,6 +1256,10 @@
return this.$el.find(selector);
},

// preinitialize is an empty function by default. You can override it with a function
// or object. preinitialize will run before any instantiation logic is run in the View
preinitialize: function(){},

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
Expand Down Expand Up @@ -1469,6 +1485,7 @@
// matched. Creating a new one sets its `routes` hash, if not set statically.
var Router = Backbone.Router = function(options) {
options || (options = {});
this.preinitialize.apply(this, arguments);
if (options.routes) this.routes = options.routes;
this._bindRoutes();
this.initialize.apply(this, arguments);
Expand All @@ -1484,6 +1501,10 @@
// Set up all inheritable **Backbone.Router** properties and methods.
_.extend(Router.prototype, Events, {

// preinitialize is an empty function by default. You can override it with a function
// or object. preinitialize will run before any instantiation logic is run in the Router.
preinitialize: function(){},

// Initialize is an empty function by default. Override it with your own
// initialization logic.
initialize: function(){},
Expand Down
25 changes: 25 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,31 @@
assert.equal(coll.one, 1);
});

QUnit.test('preinitialize', function(assert) {
assert.expect(1);
var Collection = Backbone.Collection.extend({
preinitialize: function() {
this.one = 1;
}
});
var coll = new Collection;
assert.equal(coll.one, 1);
});

QUnit.test('preinitialize occurs before the collection is set up', function(assert) {
assert.expect(2);
var Collection = Backbone.Collection.extend({
preinitialize: function() {
assert.notEqual(this.model, FooModel);
}
});
var FooModel = Backbone.Model.extend({id: 'foo'});
var coll = new Collection({}, {
model: FooModel
});
assert.equal(coll.model, FooModel);
});

QUnit.test('toJSON', function(assert) {
assert.expect(1);
assert.equal(JSON.stringify(col), '[{"id":3,"label":"a"},{"id":2,"label":"b"},{"id":1,"label":"c"},{"id":0,"label":"d"}]');
Expand Down
30 changes: 30 additions & 0 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@
assert.equal(model.get('value'), 2);
});


QUnit.test('preinitialize', function(assert) {
assert.expect(2);
var Model = Backbone.Model.extend({

preinitialize: function() {
this.one = 1;
}
});
var model = new Model({}, {collection: collection});
assert.equal(model.one, 1);
assert.equal(model.collection, collection);
});

QUnit.test('preinitialize occurs before the model is set up', function(assert) {
assert.expect(6);
var Model = Backbone.Model.extend({

preinitialize: function() {
assert.equal(this.collection, undefined);
assert.equal(this.cid, undefined);
assert.equal(this.id, undefined);
}
});
var model = new Model({id: 'foo'}, {collection: collection});
assert.equal(model.collection, collection);
assert.equal(model.id, 'foo');
assert.notEqual(model.cid, undefined);
});

QUnit.test('parse can return null', function(assert) {
assert.expect(1);
var Model = Backbone.Model.extend({
Expand Down
9 changes: 9 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
'*anything': 'anything'
},

preinitialize: function(options) {
this.testpreinit = 'foo';
},

initialize: function(options) {
this.testing = options.testing;
this.route('implicit', 'implicit');
Expand Down Expand Up @@ -181,6 +185,11 @@
assert.equal(router.testing, 101);
});

QUnit.test('preinitialize', function(assert) {
assert.expect(1);
assert.equal(router.testpreinit, 'foo');
});

QUnit.test('routes (simple)', function(assert) {
assert.expect(4);
location.replace('http://example.com#search/news');
Expand Down
22 changes: 22 additions & 0 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@
assert.strictEqual(new View().one, 1);
});

QUnit.test('preinitialize', function(assert) {
assert.expect(1);
var View = Backbone.View.extend({
preinitialize: function() {
this.one = 1;
}
});

assert.strictEqual(new View().one, 1);
});

QUnit.test('preinitialize occurs before the view is set up', function(assert) {
assert.expect(2);
var View = Backbone.View.extend({
preinitialize: function() {
assert.equal(this.el, undefined);
}
});
var _view = new View({});
assert.notEqual(_view.el, undefined);
});

QUnit.test('render', function(assert) {
assert.expect(1);
var myView = new Backbone.View;
Expand Down