开发者

Is there a better way to run jQuery UI widget methods?

开发者 https://www.devze.com 2023-02-18 07:43 出处:网络
Say you create a widget called my_widget and instantiate i开发者_StackOverflow中文版t on my_div as such:

Say you create a widget called my_widget and instantiate i开发者_StackOverflow中文版t on my_div as such:

jQuery('#my_div').my_widget()

Suppose my_widget has a do_something method. According to the widget factory documentation you would access do_something like:

jQuery('#my_div').my_widget('do_something', ['arg1','arg2'])

That's proving to be pretty cumbersome. It's error prone since if for some reason my_div doesn't yet have a widget, jQuery UI simply creates a new instance if you try to run do_something. Is there a better way?

Ideally there should be a way to say:

widget = jQuery('#my_div').my_widget('get_instance')
...
widget.do_something('arg1', 'arg2')

Is there anything like get_instance built-in?


If you read jQuery Plugin Authoring you'll see that this is the standard way to invoke a function within a plugin, and typical implementations provide no way to expose those functions as normal methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消