473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Adding custom events to widget

Hey guys!

I'm writing a widget wherein I want the pages using it to be able to
subscribe to events on it. I did my own research, not asking anyone to
write it for me, just want to make sure I'm on the right track here.

Basically, I'm creating a private collection of listeners, then
providing a mechanism for adding functions to that event.

A dispatch function loops through the listeners, finds the right one,
and calls the observer.
// Constructor
function mySampleWidget () {
// Collection of listeners (private)
var listeners = [];

// Function for adding listeners
this.addEventListener =
function (evName, observer, stopHere) {
listeners.push(
{
"event":evName,
"observer":observer,
"stop":stopHere} );
};

// the dispatch function.
this.dispatch =
function (calEvent) {
for (var i = 0; i < listeners.length; ++i) {
var listener = listeners[i];
if (listener.event == "on" + calEvent) {
listener.observer();
if (listener.stop) {
break;
}
}
}
}

mySampleWidget.prototype.show =
function() {
this.dispatch("beforeshow");
// code to display widget
this.dispatch("aftershow");
}

I'm a little worried about the dispatch method, right now it has
pseudo-protected access, which means someone could call it from outside
on a whim. I would like to make it private, but I was unable to wrap my
head all the way around Crockford's article on public/private/protected
pseudo access sufficiently.

The code works in my tests so far (unless I pass in some nulls, it
breaks real easy because I'm not type checking and making sure
everything is right before I assign.

The usage would be:

var w = new myWidget();
w.addEventListener(
"onbeforeshow",
function() {alert("Before Show");},
false};
w.show();

At this point, the application does display the proper message/call the
proper function, and does so for several different listeners, unless and
until one has a stop property that evaluates to true, and it will not
process listeners after that.

A few questions:
1. Is this a good path to be on? Is there something in there I'm doing
that's ingorant of the possible repercussions beyond the type and value
checking I've not yet implemented?

2. Can you provide any insight into how I can effectively hide the
dispatch method while still giving the other prototyped functions access
to him?

3. What other issues/caveats have you run into with this kind of thing
that I should be thinking about?

4. OT for this post, but IE7 seems not to like the
"application/javascript" type attribute. Just more of the same?

Thanks a million guys, you are appreciated.

~A!

--
Anthony Levensalor
an*****@mypetprogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 29 '07 #1
1 1599
On Dec 30, 2:46 pm, Anthony Levensalor <anth...@mypetprogrammer.com>
wrote:
David Mark said:
4. OT for this post, but IE7 seems not to like the
"application/javascript" type attribute. Just more of the same?
Use text/javascript or nothing.

That I do. It was another of those little experiments, except I wes
confusing myself with some of the new 1.8 (incomplete) docs I found, and
just started messing around. Notcied FireFox executed the code for me
and IE didn't, then went back to standard.
It's better to use nothing. That's because W3C defines it as "text/
javascript" so IE is behaving correctly according to standard. But
unhelpfully IETF decided to define the MIME type of javascripts as
"application/javascript". So, technically, web servers should serve
pages as content-type "application/javascript" but in HTML it should
be declared as "text/javascript" (which is actually a non-existent
MIME type according to IETF). That is why it's better to just say
<script></script>.
Dec 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Harlin Seritt | last post by:
I am working on making something called a PopMsg widget which is actually identical to a Balloon widget from Pmw. Here is the code: ---code--- from Tkinter import * import time class PopMsg:...
0
by: Madhusudan Singh | last post by:
Hi I am trying to add a QwtPlot widget to Qt Designer. I have both PyQwt and libqwt installed. I first tried to add a Custom Widget from /usr/include/qwt/qwt_plot.h as QwtPlot. But I do not...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.