views:

56

answers:

2
$.widget("ui.myWidget", {
   _init: function() {
      //start
      alert('Hello World!!!');
   }
}

I have the above widget/ui

when i call it like so...

<div onclick="$('#id').myWidget()">
   Click Me
</div>

i get the "Hello World!!!" alert box. if i click it again then nothing happens.

i don't know if im missing something... but i need the ui to alert each time its i click the div.

note: this is a short code to simplify and make it easy to understand....

A: 

__init is only run once. Make another function to do whatever you actually need to do. There's a good primer here

So in the head add the widget as you are now but with an extra function to do the dirty work and then change the onclick to something along the lines of $('#id').myWidget.myAlertFunction()

anq
tried that it did not work... :(
Val
that is old, confusing as hell, green1 , green2 i lost count how many greens... it doesn't explain if thats all one widget or a number of widgets to me it looks like a bunch of codes (which do not always work) even on that page when u click some of the buttons...
Val
+2  A: 

You're calling the widget in the wrong way. You want your element to "become" your widget, so you'd set your widget element like you would any other jQuery UI widget, ie. $('element').scrollable(), .draggable(), etc.

Inside your widget delcaration, you want to set all the listeners and events to each element that the widget is set to. So if you want something to fire on click of your widget element, you would set a click event to this.element, referring to the current widget element, in this case your div.

The _init function is only called once, like everyone else has said, as it initializes your widget. However, if you set it up correctly, the click event should fire on every click of your element. This should help.

HTML:

<div id="hello_world">Click Me</div>

JS:

$.widget("ui.myWidget", {
   _init: function() {
    //start
    this._show();
    var thisWidget = this;
    this.options.handler.click(function(){
        thisWidget._show();
    });
  },
  _show: function(){
    alert('Hello World???');
  }
});

$('div#click_me').click(function(){
    $('#id').myWidget({handler: $(this)});
});
munch
thats not wot i need... but i found out that if i use this ` onclick="$('#id').myWidget().myWidget('publicFoo')"`that is if the `publicFoo: function (){alert('Hello World');}` exists it will call it each time. but it looks kinda ugly...
Val
Yeah, that's not the normal behavior of a widget. Maybe you're looking for a jquery function? What else is the widget supposed to be doing?
munch
its meant to be a window like widget. like the dialog box more or less but with minimise and maximise button. so if i close the window ... then click that div:click me should create the window again from scratch (if clicked again) that is .
Val
Hmm...have you tried removing the inline onclick event and stating it in jQuery format. `$('#div_id').click(function(){ $('#id').myWidget(); });` Works for me on multiple clicks.
munch
that is weird... its not working for me :( even if i call dialog() instead of myWidget()
Val
Have you opened up firebug to see if you're getting any js errors? I notice your definition of widget does not end with `);` It may have just been copied over wrong though. Also, what does it do? Does it work only once? or not at all?
munch
i use fire bug regularly... it doesn't show any errors, and it does fire once... then nothing thereafter... same goes on ie. chrome, and firefox.
Val
intriguing. well, my last idea would be similar to what i posted above, but also similar to .draggable. You set a handler in the options to be your `'div#click_me'` and apply the click function to it (you can access it through `this.options.whatever`). I'll update the answer to show you what I mean
munch