Following on from this question, I'm trying to augment Backbone.Collection with some custom methods. However, I'm getting some inconsistent behaviour between the console and the source.
Here's how the test looks
HTML
...
<script type="text/javascript" src="./libs/underscore.js"></script>
<script type="text/javascript" src="./libs/backbone.js"></script>
<script type="text/javascript" src="./libs/backbone-extend.js"></script>
<script type="text/javascript" src="./qunit/qunit.js"></script>
<script type="text/javascript" src="./backbone-extend-tests.js"></script>
</body></html>
backbone-extend.js
Backbone.Collection.prototype.extract = function() {
// placeholder to test binding
return 'foo';
};
backbone-extend-tests.js
test('extending backbone', function () {
ok(typeof Backbone.Collection.extract == 'function');
console.log(Backbone.Collection.extract); // undefined
});
Is there something I'm missing? I've checked that all the source is loading
JFTR - this...
_.extend(Backbone.Collection, {extract:function(){return'foo';});
...works, just not using the prototype augmenting method. I'm just unsure why one method works and the other doesn't, given that the docs for Backbone recommend prototype augmentation (although it specifically mentions Models). Guess I need to take a more detailed look under the bonnet...
UPDATE: for posterity, placing this in the backbone-extend.js file...
_.extend(Backbone.Collection.prototype, {
extract : function (model) {
var _model = model;
this.remove(model);
return _model;
}
});
... works