I was attempting to extend the NodeView collection in my backbone app to override the initalize function and kept getting console errors about "Maximum call stack size exceeded" in Chrome. This is reproducable by extending the collection with something like this:
var KPIList = Drupal.Backbone.Collections.NodeView.extend({
initialize: function(opts){
KPIList.__super__.initialize.call(this, opts);
this.targets = 0;
this.targetsMet = 0;
this.bind('add', this.updateTargets, this);
},
updateTargets: function(node) {
this.targets++;
if (node.get('meets_target') == '1') {
this.targetsMet++;
}
}
});
And trying to create a new KPIList collection.
This is caused by a line in the NodeView collection's constructor:
this.constructor.__super__.initialize.call(this, opts);
This is because "this" is referencing the extended collection, so this.constructor.__super__.initialize keeps pointing towards KPIList which gets called over and over.
This patch fixes this by explicitly calling Drupal.Backbone.Collections.NodeView.__super__.initialize.call(this, opts); instead. Also fixes a tabbing issue. http://stackoverflow.com/a/12347014/2259943 has a nice explanation about this.
| Comment | File | Size | Author |
|---|---|---|---|
| fix-extending-node-view.patch | 919 bytes | acbramley |
Comments
Comment #1
ethanw commentedThe first thing that stands out to me is that the invocation of the parent constructor function using the "class object" name, not the "this" object.
Do you get the same issue if you change
KPIList.__super__.initialize.call(this, opts);tothis.__super__.initialize.call(this, opts);?Comment #2
acbramley commentedChanging it to that would only further increase the infinite loop created by using "this" please read this answer http://stackoverflow.com/a/12347014/2259943 for a full explaination
Comment #3
acbramley commentedSetting this to RTBC as I have been using it in a production system for months now