Widget factory: Changed _create to _init.

Partial fix for #5064 - Widget: make multiple instantiation more useful.
This commit is contained in:
Scott González 2010-01-15 18:58:20 +00:00
parent ab153e07c7
commit 7d96a0d096
15 changed files with 28 additions and 28 deletions

View File

@ -13,7 +13,7 @@
<script type="text/javascript"> <script type="text/javascript">
(function($) { (function($) {
$.widget("ui.combobox", { $.widget("ui.combobox", {
_init: function() { _create: function() {
var self = this; var self = this;
var select = this.element.hide(); var select = this.element.hide();
var input = $("<input>") var input = $("<input>")

View File

@ -11,14 +11,14 @@ module('widget factory', {
test('widget creation', function() { test('widget creation', function() {
var myPrototype = { var myPrototype = {
_init: function() {}, _create: function() {},
creationTest: function() {} creationTest: function() {}
}; };
$.widget('ui.testWidget', myPrototype); $.widget('ui.testWidget', myPrototype);
ok($.isFunction($.ui.testWidget), 'constructor was created'); ok($.isFunction($.ui.testWidget), 'constructor was created');
equals('object', typeof $.ui.testWidget.prototype, 'prototype was created'); equals('object', typeof $.ui.testWidget.prototype, 'prototype was created');
equals($.ui.testWidget.prototype._init, myPrototype._init, 'init function is copied over'); equals($.ui.testWidget.prototype._create, myPrototype._create, 'create function is copied over');
equals($.ui.testWidget.prototype.creationTest, myPrototype.creationTest, 'random function is copied over'); equals($.ui.testWidget.prototype.creationTest, myPrototype.creationTest, 'random function is copied over');
equals($.ui.testWidget.prototype.option, $.Widget.prototype.option, 'option method copied over from base widget'); equals($.ui.testWidget.prototype.option, $.Widget.prototype.option, 'option method copied over from base widget');
}); });
@ -30,7 +30,7 @@ test('jQuery usage', function() {
$.widget('ui.testWidget', { $.widget('ui.testWidget', {
getterSetterVal: 5, getterSetterVal: 5,
_init: function() { _create: function() {
ok(shouldInit, 'init called on instantiation'); ok(shouldInit, 'init called on instantiation');
}, },
methodWithParams: function(param1, param2) { methodWithParams: function(param1, param2) {
@ -73,7 +73,7 @@ test('direct usage', function() {
$.widget('ui.testWidget', { $.widget('ui.testWidget', {
getterSetterVal: 5, getterSetterVal: 5,
_init: function() { _create: function() {
ok(shouldInit, 'init called on instantiation'); ok(shouldInit, 'init called on instantiation');
}, },
methodWithParams: function(param1, param2) { methodWithParams: function(param1, param2) {
@ -113,7 +113,7 @@ test('direct usage', function() {
test('merge multiple option arguments', function() { test('merge multiple option arguments', function() {
expect(1); expect(1);
$.widget("ui.testWidget", { $.widget("ui.testWidget", {
_init: function() { _create: function() {
same(this.options, { same(this.options, {
disabled: false, disabled: false,
option1: "value1", option1: "value1",
@ -148,7 +148,7 @@ test('merge multiple option arguments', function() {
test(".widget() - base", function() { test(".widget() - base", function() {
$.widget("ui.testWidget", { $.widget("ui.testWidget", {
_init: function() {} _create: function() {}
}); });
var div = $("<div></div>").testWidget() var div = $("<div></div>").testWidget()
same(div[0], div.testWidget("widget")[0]); same(div[0], div.testWidget("widget")[0]);
@ -157,7 +157,7 @@ test(".widget() - base", function() {
test(".widget() - overriden", function() { test(".widget() - overriden", function() {
var wrapper = $("<div></div>"); var wrapper = $("<div></div>");
$.widget("ui.testWidget", { $.widget("ui.testWidget", {
_init: function() {}, _create: function() {},
widget: function() { widget: function() {
return wrapper; return wrapper;
} }

View File

@ -32,7 +32,7 @@ $.widget("ui.accordion", {
return this.href.toLowerCase() == location.href.toLowerCase(); return this.href.toLowerCase() == location.href.toLowerCase();
} }
}, },
_init: function() { _create: function() {
var o = this.options, self = this; var o = this.options, self = this;
this.running = 0; this.running = 0;

View File

@ -18,7 +18,7 @@ $.widget("ui.autocomplete", {
minLength: 1, minLength: 1,
delay: 300 delay: 300
}, },
_init: function() { _create: function() {
var self = this; var self = this;
this.element this.element
.addClass("ui-autocomplete ui-widget ui-widget-content ui-corner-all") .addClass("ui-autocomplete ui-widget ui-widget-content ui-corner-all")
@ -296,7 +296,7 @@ $.extend($.ui.autocomplete, {
(function($) { (function($) {
$.widget("ui.menu", { $.widget("ui.menu", {
_init: function() { _create: function() {
var self = this; var self = this;
this.element this.element
.addClass("ui-menu ui-widget ui-widget-content ui-corner-all") .addClass("ui-menu ui-widget ui-widget-content ui-corner-all")

View File

@ -27,7 +27,7 @@ $.widget("ui.button", {
secondary: null secondary: null
} }
}, },
_init: function() { _create: function() {
this._determineButtonType(); this._determineButtonType();
this.hasTitle = !!this.buttonElement.attr('title'); this.hasTitle = !!this.buttonElement.attr('title');
@ -208,7 +208,7 @@ $.widget("ui.button", {
}); });
$.widget("ui.buttonset", { $.widget("ui.buttonset", {
_init: function() { _create: function() {
this.element.addClass("ui-button-set"); this.element.addClass("ui-button-set");
this.buttons = this.element.find(':button, :submit, :reset, :checkbox, :radio, a, .ui-button') this.buttons = this.element.find(':button, :submit, :reset, :checkbox, :radio, a, .ui-button')
.button() .button()

View File

@ -48,7 +48,7 @@ $.widget("ui.dialog", {
width: 300, width: 300,
zIndex: 1000 zIndex: 1000
}, },
_init: function() { _create: function() {
this.originalTitle = this.element.attr('title'); this.originalTitle = this.element.attr('title');
var self = this, var self = this,

View File

@ -41,7 +41,7 @@ $.widget("ui.draggable", $.ui.mouse, {
stack: false, stack: false,
zIndex: false zIndex: false
}, },
_init: function() { _create: function() {
if (this.options.helper == 'original' && !(/^(?:r|a|f)/).test(this.element.css("position"))) if (this.options.helper == 'original' && !(/^(?:r|a|f)/).test(this.element.css("position")))
this.element[0].style.position = 'relative'; this.element[0].style.position = 'relative';

View File

@ -26,7 +26,7 @@ $.widget("ui.droppable", {
scope: 'default', scope: 'default',
tolerance: 'intersect' tolerance: 'intersect'
}, },
_init: function() { _create: function() {
var o = this.options, accept = o.accept; var o = this.options, accept = o.accept;
this.isover = 0; this.isout = 1; this.isover = 0; this.isout = 1;

View File

@ -17,7 +17,7 @@ $.widget("ui.progressbar", {
options: { options: {
value: 0 value: 0
}, },
_init: function() { _create: function() {
this.element this.element
.addClass("ui-progressbar" .addClass("ui-progressbar"

View File

@ -33,7 +33,7 @@ $.widget("ui.resizable", $.ui.mouse, {
minWidth: 10, minWidth: 10,
zIndex: 1000 zIndex: 1000
}, },
_init: function() { _create: function() {
var self = this, o = this.options; var self = this, o = this.options;
this.element.addClass("ui-resizable"); this.element.addClass("ui-resizable");

View File

@ -22,7 +22,7 @@ $.widget("ui.selectable", $.ui.mouse, {
filter: '*', filter: '*',
tolerance: 'touch' tolerance: 'touch'
}, },
_init: function() { _create: function() {
var self = this; var self = this;
this.element.addClass("ui-selectable"); this.element.addClass("ui-selectable");

View File

@ -31,7 +31,7 @@ $.widget("ui.slider", $.ui.mouse, {
value: 0, value: 0,
values: null values: null
}, },
_init: function() { _create: function() {
var self = this, o = this.options; var self = this, o = this.options;
this._keySliding = false; this._keySliding = false;

View File

@ -39,7 +39,7 @@ $.widget("ui.sortable", $.ui.mouse, {
tolerance: "intersect", tolerance: "intersect",
zIndex: 1000 zIndex: 1000
}, },
_init: function() { _create: function() {
var o = this.options; var o = this.options;
this.containerCache = {}; this.containerCache = {};

View File

@ -36,7 +36,7 @@ $.widget("ui.tabs", {
spinner: '<em>Loading&#8230;</em>', spinner: '<em>Loading&#8230;</em>',
tabTemplate: '<li><a href="#{href}"><span>#{label}</span></a></li>' tabTemplate: '<li><a href="#{href}"><span>#{label}</span></a></li>'
}, },
_init: function() { _create: function() {
this._tabify(true); this._tabify(true);
}, },

View File

@ -39,7 +39,7 @@ $.widget = function(name, base, prototype) {
$[namespace] = $[namespace] || {}; $[namespace] = $[namespace] || {};
$[namespace][name] = function(options, element) { $[namespace][name] = function(options, element) {
// allow instantiation without initializing for simple inheritance // allow instantiation without initializing for simple inheritance
(arguments.length && this._widgetInit(options, element)); (arguments.length && this._createWidget(options, element));
}; };
var basePrototype = new base(); var basePrototype = new base();
@ -99,7 +99,7 @@ $.widget.bridge = function(name, object) {
$.Widget = function(options, element) { $.Widget = function(options, element) {
// allow instantiation without initializing for simple inheritance // allow instantiation without initializing for simple inheritance
(arguments.length && this._widgetInit(options, element)); (arguments.length && this._createWidget(options, element));
}; };
$.Widget.prototype = { $.Widget.prototype = {
@ -108,9 +108,9 @@ $.Widget.prototype = {
options: { options: {
disabled: false disabled: false
}, },
_widgetInit: function(options, element) { _createWidget: function(options, element) {
// $.widget.bridge stores the plugin instance, but we do it anyway // $.widget.bridge stores the plugin instance, but we do it anyway
// so that it's stored even before the _init function runs // so that it's stored even before the _create function runs
this.element = $(element).data(this.widgetName, this); this.element = $(element).data(this.widgetName, this);
this.options = $.extend(true, {}, this.options = $.extend(true, {},
this.options, this.options,
@ -119,13 +119,13 @@ $.Widget.prototype = {
$.metadata && $.metadata.get(element)[this.widgetName], $.metadata && $.metadata.get(element)[this.widgetName],
options); options);
// TODO: use bind's scope option when moving to jQuery 1.4
var self = this; var self = this;
this.element.bind('remove.' + this.widgetName, function() { this.element.bind('remove.' + this.widgetName, function() {
self.destroy(); self.destroy();
}); });
(this._init && this._init(options, element)); (this._create && this._create(options, element));
(this._init && this._init());
}, },
destroy: function() { destroy: function() {