I would like to create a custom version of the sortable widget. I have been searching for documentation, but could not find something really accurate. The best information I found was : http://jqueryui.pbworks.com/Widget-factory.
I tried :
$.widget("ui.customsortable", $.extend($.ui.sortable, {
_init: function() {
$.widget.prototype._init.apply(this, arguments);
}
}));
But $.widget.prototype._init is not the function I want to call I guess since it is the $.widget prototype.
Then, I tried something I read here and there :
var _init = $.ui.sortable.prototype._init;
$.widget("ui.customsortable", $.extend($.ui.sortable, {
_init: function() {
_init.apply(this, arguments);
},
}));
But :
Am I missing something here ?
Thanks for your help !
To allow for extension, $. widget() optionally accepts the constructor of a widget to use as a parent. When specifying a parent widget, pass it as the second argument - after the widget's name, and before the widget's prototype object.
jQuery UI 1.13. 0 released | jQuery UI Blog.
a jQuery UI widget is a specialized jQuery plug-in. Using plug-in, we can apply behaviours to the elements. However, plug-ins lack some built-in capabilities, such as a way to associate data with its elements, expose methods, merge options with defaults, and control the plug-in's lifetime.
The widget factory defines how to create and destroy widgets, get and set options, invoke methods, and listen to events triggered by the widget. By using the widget factory to build your stateful plugins, you are automatically conforming to a defined standard, making it easier for new users to start using your plugins.
These are kinda strange answers. There is an optional second parameter - basewidget to inherit from. It's easy. No need to work with prototype and so on.
$.widget( "ui.customsortable", $.ui.sortable, { _init: function() { this.element.data('sortable', this.element.data('customsortable')); // or whatever you want } } );
The second parameter is $.ui.sortable. I think it's all you need.
After several tries, I finally found out how to do this easily :
$.widget("ui.customsortable", $.extend({}, $.ui.sortable.prototype, { _init: function(){ this.element.data('sortable', this.element.data('customsortable')); return $.ui.sortable.prototype._init.apply(this, arguments); } // Override other methods here. })); $.ui.customsortable.defaults = $.extend({}, $.ui.sortable.defaults);
The key is to copy data from your custom widget to the original one. Don't forget to use $.ui.sortable.prototype.[overriden method].apply(this, arguments); in each overriden method.
Holly crap !
Regarding the selected solution above:
$.widget("ui.customsortable", $.extend(true, {}, $.ui.sortable.prototype, {
If you are extending one objects options into another, the [deep] flag of true will give you the desired results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With