尝试扩展小部件时出现错误,为什么?
I get an error when trying to extend a widget, why?
这里有两个小部件。
如果我尝试从 'sb.baseApp' 扩展 'sb.textboxesApp',我会得到一个错误:
"TypeError: s is not a constructor"
但是为什么???
这不是扩展小部件的方式吗?
jquery 1.8.3
jquery ui 1.10.3
我认为您应该将小部件本身作为 .widget() 的第二个参数传递。不是字符串。这是我的测试。 http://jsfiddle.net/ho873kz4/
/****************************
Base Widget
Requires jQuery
Requires jQuery UI
****************************/
(function($, undefined) {
'use_strict';
/*
* A widget.
*/
$.widget('sb.baseApp', {
/*
* Widget Option
*/
options: {},
/*
* Constructor, called once when first created
*/
_create: function() {
console.log('baseApp created');
},
/*
* Initialisation, called after _create and and on later widget calls.
*/
_init: function() {
},
/*
* Destructor, cleanup when no longer needed.
*/
_destroy: function() {
}
});
} (jQuery));
/****************************
Textbox Widget
Requires jQuery
Requires jQuery UI
****************************/
(function($, undefined) {
'use_strict';
/*
* A widget
*/
$.widget('sb.textboxesApp', $.sb.baseApp, {
/*
* Widget Option
*/
options: {},
/*
* Constructor, called once when first created
*/
_create: function() {
console.log('textboxesApp create');
return this._super();
},
/*
* Initialisation, called after _create and and on later widget calls.
*/
_init: function() {
},
/*
* Destructor, cleanup when no longer needed.
*/
_destroy: function() {
}
});
} (jQuery));
jQuery.sb.textboxesApp();
这里有两个小部件。
如果我尝试从 'sb.baseApp' 扩展 'sb.textboxesApp',我会得到一个错误:
"TypeError: s is not a constructor"
但是为什么???
这不是扩展小部件的方式吗?
jquery 1.8.3 jquery ui 1.10.3
我认为您应该将小部件本身作为 .widget() 的第二个参数传递。不是字符串。这是我的测试。 http://jsfiddle.net/ho873kz4/
/****************************
Base Widget
Requires jQuery
Requires jQuery UI
****************************/
(function($, undefined) {
'use_strict';
/*
* A widget.
*/
$.widget('sb.baseApp', {
/*
* Widget Option
*/
options: {},
/*
* Constructor, called once when first created
*/
_create: function() {
console.log('baseApp created');
},
/*
* Initialisation, called after _create and and on later widget calls.
*/
_init: function() {
},
/*
* Destructor, cleanup when no longer needed.
*/
_destroy: function() {
}
});
} (jQuery));
/****************************
Textbox Widget
Requires jQuery
Requires jQuery UI
****************************/
(function($, undefined) {
'use_strict';
/*
* A widget
*/
$.widget('sb.textboxesApp', $.sb.baseApp, {
/*
* Widget Option
*/
options: {},
/*
* Constructor, called once when first created
*/
_create: function() {
console.log('textboxesApp create');
return this._super();
},
/*
* Initialisation, called after _create and and on later widget calls.
*/
_init: function() {
},
/*
* Destructor, cleanup when no longer needed.
*/
_destroy: function() {
}
});
} (jQuery));
jQuery.sb.textboxesApp();