Reputation: 50362
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?
Upvotes: 4
Views: 1308
Reputation: 16020
The way you're extending Backbone.Model
is not how you want to go about it. If you want to create a new type of model, just use extend
:
Backbone.New_Plugin.Model = Backbone.Model.extend({
sync: function(method, model, options){
alert('Body of sync method');
}
});
var newModel = Backbone.New_Plugin.Model.extend({
// custom properties here
});
var newModelInstance = new newModel({_pk: 'primary_key'});
On another note, Crockford's Object.create
polyfill is considered outdated because (I believe) more recent implementations of Object.create
take more than one argument. Also, the particular function you're using does not defer to a native Object.create
function, should it exist, although, you may have just omitted the if (typeof Object.create !== 'function')
statement that should wrap that function.
Upvotes: 6