// Publish the subscription object
var eventObj = {
// Caches the list of subscribers
list: {},// Add a subscription
listen:function(key,fn){
if(!this.list[key]){
this.list[key] = [];
}
typeof fn==='function' && this.list[key].push(fn);
},
// Publish information
trigger:function(){
// Retrieve the color color
// var key = [].shift.call(arguments);
var key = Array.prototype.shift.call(arguments);
// var key = Array.from(arguments).shift();
var fns = this.list[key];
for(var i=0,len=fns.length; i<len; i++) {var fn = fns[i];
// fn();
fn.apply(this.arguments); }},// Unsubscribe
removeListen(key,fn){
var fns = this.list[key];
// The subscription type does not exist, and the subscription is not passed in for processing callback
if(! fns) {return;
}
if(typeof fn==='undefined') {return;
}
var sindex = fns.indexOf(fn);
fns.splice(sindex,1); }}var ajaxObj = Object.create(eventObj);
ajaxObj.listen('success'.function(data){
console.log(data);
})
function getData(){
xmlAjax({
method:'post'.url:'http://localhost:3000/api/GetUser'.done:function(data){
ajaxObj.trigger('success', {fromData1:data }); }}); } getData();Copy the code