English 中文(简体)
jj 查询事件: 通知过去事件中新建的元素
原标题:jQuery events: notify the newly created elements of past events

代码示例:

var Events = $({});

Events.on( myCustomEvent , function(){ console.log( myCustomEvent 1 ) })

Events.trigger( myCustomEvent ); // I want notify the previous binds and all binds in future

$.ajax({...}).success(function(res){
    Events.on( myCustomEvent , function(){ console.log( myCustomEvent 2 ) })
});

// when ajax or other long process will be finished  myCustomEvent  must be fired

myCustomEvent 1 will be printed because its binded before trigger will called and myCustomEvent 2 must be trigged too

这是在jQuery的解决方案吗?

最佳回答

看起来你所要的不是很多事件, 而是一个酒吧/子系统, 或者一个执行观察者模式的代码。 大多数这些执行都不具备在信息发布后回拨订户的能力。 我做了, 但你需要小心, 使用这个系统的人知道一个信息可能会被多次发布。 不幸的是, 我无法分享这个代码, 因为它属于我的雇主所有 。

只是在 JavaScript 或 JavaScript 中搜索“ 观察者模式” 或“ pubsub” 或“ pubsub in JavaScript ”, 就能为您找到一个良好的图书馆。 通常, 这些图书馆会使用一个“ 主题” 的散列, 来设置一系列函数, 当该主题被发布时, 这些函数会被称为“ 主题 ” 。 关键在于保存发布时的历史。 当某人订阅一个已经发布的主题时, 重新发布该主题 。

希望这足够让你开始

问题回答

我认为解决办法是 美元回响

Callbacks.highlight = $.Callbacks( memory )

// ComponentB
$(window).hashchange(function(){

    $.ajax({ /* load items of #ListB */ }).success(function(data){

        /* append items to dom */

        if (/listA=123/.test(location.hash))
            Callbacks.highlight.fire();
    })

    $( #ListB ).on( click ,  a , function(){ /* highlight the same in #ListA by alg2 */ })
})

// ComponentA
$(window).hashchange(function(){

    $.ajax({ /* load items of #ListA */ }).success(function(data){

        /* append items to dom */
        Callbacks.highlight.add( /* highlight item */ )
    })

    $( #ListA ).on( click ,  a , function(){ /* highlight the same in #ListB by alg1 */ })
})




相关问题
Listen for Events from Browser "Find" Window in JavaScript

Is there a way to listen for typing into the browser s "find" window in JavaScript? (source: apple.com) I d like to be able to reinterpret the search text from JavaScript. What do I need to ...

Equivalent of PreviewKeyDown of C# needed for native C++/MFC

I used a ActiveXControl in a C# Forms application and had the possibility to implement a PreviewKeyDown event handler for that ActiveXControl. That was needed to specify that, e.g. the [ALT] key, is ...

Refresh the UI in gwt

I have created some custom composite widget, which listens to an events (in my case loginEvent). Once the event is caught the state of the widget changes so as the way it should look (in my case I ...

How to remove "onclick" with JQuery?

PHP code: <a id="a$id" onclick="check($id,1)" href="javascript:void(0)" class="black">Qualify</a> I want to remove the onclick="check($id,1) so the link cannot be clicked or "check($id,...

热门标签