Showing posts with label raise. Show all posts
Showing posts with label raise. Show all posts

Monday, March 26, 2012

Listeners Broadcast in Javascript/ Atlas Library

Is there a framework in place for a Atlas JS Control to raise an eventwhich can have other Controls or global functions listen in on andhandle?

Just like in Web User Control, we can raise Events which can be handledby pages or other controls that 'listens' in on the event, can the samebe accomlished in JS?

The Web User Control shouldnt know who has registered to listen in onthe event but somehow the Control has to communicate that something hashappened.Would any of the below code help in what I want to accomplish?Basically different components can listen in or another componentsevents, and when that event is raised, each of the listeners methodsare invoked?


Type.createEnum('Sys.ActionSequence','BeforeEventHandler', 0,'AfterEventHandler', 1);
Sys.IAction = function() {
this.get_sequence = Function.abstractMethod;
this.execute = Function.abstractMethod;
this.setOwner = Function.abstractMethod;
}
Sys.IAction.registerInterface('Sys.IAction');

Type.Event = function(owner, autoInvoke) {
var _owner = owner;
var _handlers =null;
var _actions =null;
var _autoInvoke = autoInvoke;
var _invoked =false;

this.get_autoInvoke = function() {
return _autoInvoke;
}

this._getActions = function() {
if (_actions && _actions.length && !_owner)throw"Actions are only supported on events that have an owner.";
if (_actions ==null) {
_actions = [];
}
return _actions;
}
this._getHandlers = function() {
if (_handlers ==null) {
_handlers = [];
}
return _handlers;
}
this._getOwner = function() {
return _owner;
}

this.isActive = function() {
return ((_handlers !=null) && (_handlers.length != 0)) ||
((_actions !=null) && (_actions.length != 0));
}

this.get_isInvoked = function() {
return _invoked;
}

this.dispose = function() {
if (_handlers) {
for (var h = _handlers.length - 1; h >= 0; h--) {
_handlers[h] =null;
}
_handlers =null;
}
if (_actions) {
for (var i = _actions.length - 1; i >= 0; i--) {
_actions[i].dispose();
}
_actions =null;
}

_owner =null;
}

this._setInvoked = function(value) {
_invoked =true;
}
}
Type.Event.registerSealedClass('Type.Event',null, Sys.IDisposable);

Type.Event.prototype.add = function(handler) {
this._getHandlers().add(handler);
if (this.get_autoInvoke() &&this.get_isInvoked()) {
handler(this._getOwner(),null);
}
}
Type.Event.prototype.addAction = function(action) {
action.setOwner(this._getOwner());
this._getActions().add(action);
}
Type.Event.prototype.remove = function(handler) {
this._getHandlers().remove(handler);
}
Type.Event.prototype.removeAction = function(action) {
action.dispose();
this._getActions().remove(action);
}
Type.Event.prototype.invoke = function(sender, eventArgs) {
if (this.isActive()) {
var actions =this._getActions();
var handlers =this._getHandlers();
var hasPostActions =false;
var i;

for (i = 0; i < actions.length; i++) {
if (actions[i].get_sequence() == Sys.ActionSequence.BeforeEventHandler) {
actions[i].execute(sender, eventArgs);
}
else {
hasPostActions =true;
}
}

for (i = 0; i < handlers.length; i++) {
handlers[i](sender, eventArgs);
}

if (hasPostActions) {
for (i = 0; i < actions.length; i++) {
if (actions[i].get_sequence() == Sys.ActionSequence.AfterEventHandler) {
actions[i].execute(sender, eventArgs);
}
}
}

this._setInvoked();
}
}


Sorry for the message, I forgot to check the Atlas Client sidereference library where it has a nice page on this:http://atlas.asp.net/docs/Client/Type/Event/default.aspx

but anyone with more concrete examples would be much appreciated :)
Hi,

the Atlas framework gives you the ability to declare multiple handlers for an event. Just use the add() method on the exposed event to add a handler.

For example, if the instance myObject exposes a myevent event, you can write a statement like:

myObject.myevent.add(myMethod);

to register a handler for that event.
Thanks for your response Garbin (+ props to your weblog)

Looking at your pager example:

// Events.
// The Pager exposes the pageChanged event, which is raised
// when the page index to be displayed changes.
this.pageChanged =this.createEvent();
...

Which exposes a pageChanged property where I can pass in one method on the initialization of the Atlas Control.

td.addEvent('pageChanged',true);

...

The event is raised by:

this.pageChanged.invoke(this,new AtlasNotes.PagerEventArgs(_pageIndex, _pageSize, _totalRecords));

so going by your post, I can append more methods to be handled by going:

myObject.pageChanged.add(myMethod);

If so, Ill try it on Monday :D


The above code works beautifully :)

Listening for propertyChanged events

I have seen several examples on how to raise an event when a property changes, but I haven't been able to find any examples on how to start listening for it. How would I listen for an event raised through this:

this.raisePropertyChanged('myProperty');

I guess I would have to use the $addHandlers method, but what is the name of this specific event?

you do:

someObj.add_propertyChanged(SomeHandlerMethod);

Cheers


Ehm... where does it say which property I'm listening for?


See if you have anything being passed as parameter in the method handler.

function SomeHandlerMethod(sender, args)
{

}


So what you are basically saying is that if I want to listen for a specific property to change, I will have to listen for all properties that change, and then check whch ones then actually did change?