Jos*_*man 8 javascript jquery abstraction jquery-ui class
我正在尝试为jQuery UI开发一个抽象层,它允许将Widgets定义为与ExtJS类似(或类似)的对象.这是概念:
var mydialog = new $.ui.dialog({
modal:true,
renderTo:'body',
title:'The Windows Tittle',
content:'The content of the Window'
});
Run Code Online (Sandbox Code Playgroud)
现在我可以说:
mydialog.show();
Run Code Online (Sandbox Code Playgroud)
第一步(我认为)是为jQuery添加一个类创建函数,这允许创建类:
$.MYNAMESPACE.dialog = $.Class ({
constructor:function(){}
//methods and properties
});
Run Code Online (Sandbox Code Playgroud)
这里出现了真正的问题:我必须在上一个类定义中将真正的$ .ui.dialog与我的链接联系起来?我的意思是我不想创建一个新的小部件,我只想重用预定义的jQuery UI小部件后面的代码,以创建一个允许带有jQuery UI的OOP的抽象层.
提前致谢
你尝试过 jquery-ui 小部件工厂吗?您可能正在重新发明wheel.js
幻灯片展示您通过小部件工厂获得的成果
快速了解它在做什么。我想要一个包含一些自定义事件的新对话框
//this is instantiating the widget, does not need to be in the same file
$(function(){
$(".some-selector").miDialog({autoopen:true //because we can});
});
//this is a definition, not an instantiation of the widget. aka,
$.widget("mi.miDialog" //namespace
,$.ui.dialog //inherit from this jquery widget
,//start your widget definition
{ options:{autoopen:false,//overwrite parent default option, while still giving instance option to override our definition's override of parent
someInstanceSafeOption: {why:"not",have:"a",subobject:"option"} },
//underscore functions are 'private' unless you dig into the prototype manually
_create :function(){
//you'll need this function. guaranteed to run once.
// upcoming version call parent create
this._super();
//current version call parent create
$.ui.dialog.prototype._create(this.options);
this.element.addClass("mi-dialog"); //help with custom styling
this._trigger("created"); //trigger custom events
//register for events here, as _create() will only run once per individual instance
},
_init:function(){
//this will run every time someone calls $('some-selector').miDialog();
//i have yet to use it much
},
publicFunction: function(some, params){
//this function does whatever you want, and is called $('some-selector'.miDialog("publicFunction", some,params);
},
_destroy: function(){
//clean up after your widget's create function, if needed.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
812 次 |
| 最近记录: |