Knowledge points:
1. Custom events and event subscriptions
2. Use of trigger, bind, and unbind methods
3. Customization with namespace Event
Paste the following content into the txt document and then paste it directly into the firebug console and run it.
/*
JQuery custom event application- Javascript OO
There is a handsome guy named Levin, who works in the front-end design department of a company. His supervisor is handsome guy A, and his manager is pretty girl B.
The leaders wanted to take the time to take a look at this guy’s work every week, so. . .
Levin has to use Google Doc to write a summary of last week's work every Monday, and is required to notify A and B on MSN after finishing the writing. . .
Levin is very happy to do the weekly summary, but he made a suggestion:
Since I have written it in Google Doc, if you like to read it, you can use Google Reader to subscribe to my document updates. I don’t like to use Microsoft’s MSN. . . .
Here...A and B had crows floating above their heads for a few seconds, but they readily agreed.
*/
//This is classmate Levin
var Levin={name:"Levin"};
Levin=$(Levin);
//Write a weekly summary every week
Levin.bind("evt_weeklyReport",function(evt){
alert(this.name "Shout: Yeah! The weekly summary has been written.");
});
//Supervisor A
var A={name:"Handsome Guy A"};
//He wants to subscribe to Levin’s weekly summary
A.rssLevin=function(){
Levin.bind("evt_weeklyReport.fromA",function(evt,data){
alert(A.name ": Well, not bad, Levin is quite positive) ! ");
})
};
A.rssLevin();
//Manager B
var B={name:"Meimei B"};
// She also subscribes to Levin's weekly summary
B.rssLevin=function (){
Levin.bind("evt_weeklyReport.fromB",function(evt,data){
alert(B.name ": Weekly summary will be read later, first take a look at the website he mentioned");
window.location=evt.site;
})
};
B.rssLevin();
//Every time Levin writes After the weekly summary, I will publish it using google doc
Levin. trigger({type:"evt_weeklyReport",site:"http://jb51.net"});
//Manager B suddenly went to Sohu one day and stopped subscribing Levin's weekly summary...
[code]
B.unRssLevin=function(){
alert("I'm going to Sohu to be the vice president, haha");
Levin.unbind( "evt_weeklyReport.fromB");
return true;
}();
[html]
//Although B has changed jobs, the weekly summary still needs to be released on time. . .
Levin.trigger({type: "evt_weeklyReport",site :"http://jb51.net"});