ASP.Net ScriptControl - Add the Javascript get_events method : Possible? - asp.net

So I've run into a snag, apparently the get_events method is only "included" with the ExtenderControl class.
What I need to do:
Be able to call the get_events Javascript method using a ScriptControl since using an ExtenderControl isn't really possible at this point.
I am hoping there is an easy way to have the scriptControl's javascript object inherit from something (If that's even possible).

Turns out the get_events method is really simple to create. You just need a field to hold a dictionary, a couple lines of code, and something to call the event if needed:
getEvents: function()
{
if (this._events == null)
{
this._events = new Sys.EventHandlerList();
}
return this._events;
},
And now for access:
onItemSelected: function(args)
{
this.raiseEvent('userItemSelected', args);
},
raiseEvent: function(eventName, eventArgs)
{
var handler = this.getEvents().getHandler(eventName);
if(handler)
{
handler(this._autoComplete, eventArgs);
}
},
Basically events is just a dictionary that holds the name of the event and the reference to the method to call. Once you have the method (handler), it's just a matter of calling it.

Related

Can I override/extend Meteor methods?

Is it possible to somehow override a method in Meteor?
Or define another function such that both will get called?
In my regular code:
Meteor.methods(
foo: (parameters) ->
bar(parameters)
)
Somewhere else that gets loaded later (e.g. in tests):
Meteor.methods(
# override
foo: (parameters) ->
differentBehavior(parameters)
# I could call some super() here
)
So I would expect to either have both bar and differentBehavior executed or only differentBehavior and some possibility to call super().
Does this exist?
To override a method, on server side you can do:
Meteor.methods({
'method_name': function () {
//old method definition
}
});
Meteor.default_server.method_handlers['method_name'] = function (args) {
//put your new code here
};
The Meteor.default_server.method_handlers['method_name'] has to be included after the method definition.
To override a method (also know as a stub), on client side you can do:
Meteor.connection._methodHandlers['method_name'] = function (args) {
//put your new code here
};
The Meteor.connection._methodHandlers['method_name'] has to be included after the method definition.
There are lots of ways you can do what you are intending to do.
For instance, the simplest way to overwrite any function would be to do something like:
Meteor.publish = function() { /* My custom function code */ };
We just over-wrote the Meteor.publish with our own instance.
However, if you want to wrapper a function like a proxy (I believe this is called a "proxy pattern":
var oldPublish = Meteor.publish();
Meteor.publish = function() {
oldPublish(arguments); // Call old JS with arguments passed in
}
ES6 also added a Proxy object that allows you to do some similar things (read about it here).
Also there are lots of AOP libraries (CujoJS, jQuery-AOP, and node-aop to name a few) for JavaScript that allow you to do before, after, around pointcuts on functions/objects. You could even roll-your-own if you wanted too.

Flex: Which way should I add this event handler?

I use a unit of work pattern a lot in my flex projects. I'll have a class that might call a web service, put the data in a sqlite db, refresh a model with the data then raise an event.
I usually call these inline and pass in some singleton classes:
protected function CareerSynced():void
{
var process:ProcessWorkouts = new ProcessWorkouts(_dataModel, _trainerModel, _databaseCache, _database.Conn);
process.addEventListener("AllWorkoutsProcessed", AllWorkoutsProcessed);
process.UpdateAllUnprocessed();
}
I'll then get the response like this:
private function AllWorkoutsProcessed(event:DataReceivedEvent):void
{
//do something here
}
My question is, am I adding that event listener correctly? I think I might be causing a memory leak, but I'm not sure. I've also thought about using a weak reference. I'm confused about when to use them. Would this be one of those cases?
Should it be like this?
process.addEventListener("AllWorkoutsProcessed", AllWorkoutsProcessed,false, 0, true);
I would either go with the weak reference or just remove the listener:
private function AllWorkoutsProcessed(event:DataReceivedEvent):void
{
event.target.removeEventListener("AllWorksoutsProcessed",AllWorkoutsProcessed);
}
I could list out my reasons but I'll just point you to this.

Flex Event Dispatching

I have some questions with a particular structure of a program I'm writing.
I'm using a Remote Object to make a remote call to a Rails method (using WebOrb). The problem arises in the way that I get my data back.
Basically I have a function, getConditions, in which I add an event listener to my remote call and then I make the remote call. However, what I want to do is to get that data back in getConditions so I can return it. This is a problem because I only access the event result data in the event handler. Here's some basic code describing this issue:
public function getConditions():Array
{
remoteObject.getConditions.addEventListener("result", onConditionResult);
remoteObject.getConditions();
//Here is where I want to get my event.result data back
}
public function onConditionResult(event:ResultEvent):void
{
//Here's the data that I want
event.result;
}
How can I achieve this data turn-about?
Remote calls in flex are always asynchronous so you won't be able to call getConditions() and wait there for the result. You have to use a function closure to process the results, either by means of an event handler than you declare elsewhere or a dynamic one created immediately within getConditions(), like so:
remoteObject.getConditions.addEventListener("result", function(event:ResultEvent):void {
// Run the code that you would want to when process the result.
});
remoteObject.getConditions();
The advantage of doing the above is that you would be able to "see" parameters passed to getConditions() or the result of any logic that happened before addEventListener() in the function closure. This however, takes a slight performance hit compared to declaring an explicit function (for that exact reason).
I should also add that doing so requires you to clean up after yourselves to make sure that you are not creating a new listener for every request.
you do it like this
public function getConditions():Array
{
remoteObject.getConditions.addEventListener("result", onConditionResult);
remoteObject.getConditions();
}
public function callMyExtraFunction(data:Object):void
{
//Here is where you want to get your event.result data back
}
public function onConditionResult(event:ResultEvent):void
{
//Here's the data that you want
var data:Object = event.result;
callMyExtraFunction(data);
}
You could make use of Call Responder like so :
<s:CallResponder id="getOperationsResult"/>
then use these lines to get the result from get operations
getOperationResult.token = remoteObject.getOperation();
this creates the call and returns the result stores it in getOpresult
whnever u want to access this u can call that token or getOperationResult.lastResult
Hope that helps
Chris

Calling Javascript After Web Method has fired

I'm trying to implement the cascading dropdown from the toolkit. I need to get the count in a sub category dropdown, if it's zero then I turn off the visibility of the sub category.
If I use the javascript OnChange event then my script fires before the web method, so I need to know how to fire my script AFTER the web method has fired please.
My demo page: http://bit.ly/92RYvq
Below is my code and the order I need it to fire.
[WebMethod]
public CascadingDropDownNameValue[] GetSubCats1(string knownCategoryValues, string category)
{
StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
int CategoryID;
if (!kv.ContainsKey("Category") || !Int32.TryParse(kv["Category"], out CategoryID))
{
return null;
}
dsSubCat1TableAdapters.Categories_Sub1TableAdapter SubCats1Adapter = new dsSubCat1TableAdapters.Categories_Sub1TableAdapter();
dsSubCat1.Categories_Sub1DataTable SubCats1 = SubCats1Adapter.GetSubCats1(CategoryID);
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
foreach (DataRow dr in SubCats1)
{
values.Add(new CascadingDropDownNameValue((string)dr["SubCategory1"], dr["SubCatID1"].ToString()));
}
return values.ToArray();
}
function getSubCatCount() {
$get("ddlSubCats1").style.display = $get("ddlSubCats1").length > 1 ? "block" : "none";
}
Generally when you call your web method function through javascript you can supply it two call back functions. One gets fired if there is an error and the other gets fire once the web method call has been completed. The callback requires two arguments, the result and a context. For example if your function was called myWebMethodFunction and your namespace it was contained in was my.fully.qualified.namespace it may look like this.
my.fully.qualified.namespace.myWebMethodFunction(param1, param2, ... , paramN, onErrorCallback, onCompleteCallback, context);
Once that function finishes, it will call the onCompleteCallback passing the result of your webmethod function and whatever you passed for a context.
It has been a while since I've called a web method function so I might have gotten the order of the callback reversed.
For some reason I can't comment on things either, but I can add to this.
I may be thinking about something different, but you must be calling something through javascript to fire your webmethod, correct? Whatever you use to call the webmethod through javascript should provide a mechanism to add a callback that will be fired once your webmethod call is complete and returned.
Use jQuery.ajax() it allows you to specify a success function and a failure function that fire after the web method returns.

What is the use of raisePropertyChanged Event?

I couldn't understand the purpose of this event from official documantation.
It's commonly used developing controls with clint support (IScriptControl).
get_highlightCssClass: function() {
return this._highlightCssClass;
},
set_highlightCssClass: function(value) {
if (this._highlightCssClass !== value) {
this._highlightCssClass = value;
this.raisePropertyChanged('highlightCssClass');
}
},
Is it used to update the server's-side property from the clint side?
How do I catch this event on server side and get the updated property value?
This article by Garbin explains the use of this (and more).
[Edit to show sample usage]
Suppose you have this in an instance of classA inside ClassB, then you add the following to ClassB:
classA.add_propertyChanged(onPropChanged);
function onPropChanged(sender, e) {
if (e.get_propertyName == 'highlightCssClass') {
// Do something with this....
}
}
[/End Edit]
This event helps you to create observable objects, that is, objects whose changes in state that you can track. Useful for example when using LINQ to SQL, in orde to know which entities have been changed and need to be sent back to the database.

Resources