Plugin creation

Trying to create a basic "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'); } 

Method: Object.create() was taken directly from Javascript: The Good Parts :

 Object.create = function(o){ var F = function(){}; F.prototype = o; return new F(); }; 

I get 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 Backbone 0.9.2 development version. inside the inherits() function:

  Uncaught TypeError: Function.prototype.toString is not generic.

I am trying to create a new plugin so that the Marionette base library creates new versions of Views. It seems to me that I do not understand how this should be done.

What is a good way to create a new trunk plugin?

+4
source share
1 answer

The way to extend Backbone.Model is not how you want to do it. If you want to create a new model type, simply 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 the other hand, Crockford Object.create polyfill is deprecated because (I believe) later implementations of Object.create take more than one argument. Also, the specific function you use does not defer to the built-in Object.create function if it exists, although you can simply omit the if (typeof Object.create !== 'function') , which should wrap this function.

+6
source

All Articles