Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  47] [ 1]  / answers: 1 / hits: 18426  / 9 Years ago, sun, april 12, 2015, 12:00:00

Is there a way in Javascript to have a delegate like the ones in c# ?



Example in c#



Object.onFunctionCall = delegate (vars v) {
Console.WriteLine(I can do something in this private delegate function);
};


I would like with my Javascript to have my main object do something over a long time, and shot a delegate once in a while to give a little update. All that without having to change the code itself of my class to adjust for the webpage.



function mainObject() {
this.onUpdate = function() { //Potentially the delegate function here
}
}

var a = new mainObject();
a.onUpdate = Delegate {
$(.myText).text(Just got a delegate update);
}


I dunno if it's clear enough.. havent found ressources on this so I suppose there is just no way to do so ?



NOTE: I am not looking into jquery Click delegates event here, but into delegating a function call like how it works in c#



Let me know


More From » delegates

 Answers
36

What you are looking for is an Observer Pattern, as described eg. here.



But as you are interested in jQuery, you don't need to go the trouble of writing an observer pattern for yourself. jQuery already implements an observer in the guise of its .on() method, which can be invoked on a jQuery collection to cause callback function(s) to fire every time a native or custom event is dispatched.



Here's an example :



$(function() {
//attach a custom event handler to the document
$(document).on('valueChange', function (evt) {
$(this).find(#s0).text(evt.type);
$(this).find(#s1).text(evt.value);
$(this).find(#s2).text(evt.change);
$(this).find(#s3).text(evt.timestamp).toLocaleString();
});

//customEvent(): a utility function that returns a jQuery Event, with custom type and data properties
//This is necessary for the dispatch an event with data
function customEvent(type, data) {
return $.extend($.Event(type||''), data||{});
};

//randomUpdate(): fetches data and broadcasts it in the form of a 'changeValue' custom event
//(for demo purposes, the data is randomly generated)
function randomUpdate() {
var event = customEvent('valueChange', {
value: (10 + Math.random() * 20).toFixed(2),
change: (-3 + Math.random() * 6).toFixed(2),
timestamp: new Date()
});
$(document).trigger(event);//broadcast the event to the document
}
});


Here's a demo, complete with start and stop buttons for a regular interval dispatch of the custom event.



Notes




  • Under some circumstances, it might be more appropriate to broadcast the event to the four data spans individually.

  • On the web, you will find mention of a more convenient jQuery.event.trigger({...}) syntax. Unfortunately this was an undocumented feature of jQuery, which disappeared at v1.9 or thereabouts.


[#67104] Thursday, April 9, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stefanicarolinat

Total Points: 145
Total Questions: 91
Total Answers: 93

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
stefanicarolinat questions
Mon, Nov 15, 21, 00:00, 3 Years ago
Fri, Apr 16, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Fri, Jul 17, 20, 00:00, 4 Years ago
;