Trying to make a plugin for jquery. Here is its most simplified version:
(function($) { $.extend({ VK: function(options) { this.init = function() { } this.init(); // allow jQuery chaining return this; } }); })(jQuery); At the moment my plugin is being called in the following way:
$.VK(); I want to create an event handler in the style as implemented, for example, for ajax:
$.VK().done(function( data ) { }).fail(function( data ){ }); How can this be implemented? That is, the question is how to call the done () function from the plugin?
I think you need something like this, but it certainly does not work
Jsfiddle
(function($) { $.extend({ VK: function(options) { this.init = function() { var someCondition = true; if (someCondition) { this.done("OK") } else { this.fail("fail") } } this.init(); // allow jQuery chaining return this; } }); })(jQuery);
doneandfailare methods of aPromiseor similar jQuery object. Therefore, to use them, theVKfunction must return apromiseor an object in which there are these functions - Grundy$.VK()returns a globaljQueryobject to which you also add theinitmethod. - Igorvar result = {init: function(){...}, done: function(){...; return this; } fail: function(){...; return this;} }; return result;And callbacks in the code are not yet visible. - Igor