Trying to create a backbone “plugin” that “inherits” from Backbone.Model but overrides the sync method.
This is what I have so far:
Backbone.New_Plugin = {};
Backbone.New_Plugin.Model = Object.create(Backbone.Model);
Backbone.New_Plugin.Model.sync = function(method, model, options){
alert('Body of sync method');
}
The method: Object.create() was taken directly from the book Javascript: The Good Parts:
Object.create = function(o){
var F = function(){};
F.prototype = o;
return new F();
};
I’m getting an error when trying to use the new model:
var NewModel = Backbone.New_Plugin.Model.extend({});
// Error occurs inside backbone when this line is executed attempting to create a
// 'Model' instance using the new plugin:
var newModelInstance = new NewModel({_pk: 'primary_key'});
The error occurs on line 1392 of the development version of Backbone 0.9.2. inside the function inherits():
Uncaught TypeError: Function.prototype.toString is not generic .
I am trying to create a new Plugin in the way that the backbone library Marionette creates new versions of Views. IT looks like I am misunderstanding the way that this should be done.
What is a good way to create a new plugin for backbone?
The way you’re extending
Backbone.Modelis not how you want to go about it. If you want to create a new type of model, just useextend:On another note, Crockford’s
Object.createpolyfill is considered outdated because (I believe) more recent implementations ofObject.createtake more than one argument. Also, the particular function you’re using does not defer to a nativeObject.createfunction, should it exist, although, you may have just omitted theif (typeof Object.create !== 'function')statement that should wrap that function.