第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Observer pattern in JavaScript

標簽:
JavaScript
The observer pattern (a subset of the asynchronous publish/subscribe pattern) is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

Many times, i saw the observer word. For example, in extjs there is a Ext.util.Observable class.

Abstract base class that provides a common interface for publishing events. Subclasses are expected to to have a property “events” with all the events defined.

I look at the code and find something like this:

Ext.util.Observable = function() { };
Ext.util.Observable.prototype = (function() {       

    return {   

          addListener : function(eventName, fn, scope, o){   

          },   

          removeListener : function(eventName, fn, scope){   

          }   

    };   

})();

Seems it just an abstract class to maintain functions relation to events.
I didn’t understand the inside logic of observer until i saw this article.
It’s about new feature of ASP.NET4.0 AJAX PREVIEW - JavaScript Observer Pattern, implemented in Sys.Observer.

First, there is an old-plain javascript object:

var latestPost = {title:"JavaScript Observer Patter", url:"..."};
function initComponent() {
    $("").attr("href", latestPost.url).html(latestPost.title).appendTo("body");
}

We use this object to create a hyperlink in the DOM.
Second, After a period of time, we retrieve the latest post from server and update latestPost object. The question is how to notify the iniComponent function to execute again?

With the help of Sys.Observer in ASP.NET 4.0 AJAX PREVIEW:

Sys.Observer.makeObservable(latestPost);
Sys.Observer.addPropertyChanged(latestPost, iniComponent);
latestPost.beginUpdate();
latestPost.setValue("title", "I want an iphone.");
latestPost.endUpdate();

When you call the endUpdate function, the function iniComponent registered in lastedPost object has to be execute again.

 You see, the Sys.Observer’s logic should not much complex, which manage event callbacks and add some functions to the plain-old object. When the functions are called, just trigger the callbacks.

It’s clear - Observer, right?

點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消