Skip to content

Instantly share code, notes, and snippets.

Created August 8, 2016 19:30
Show Gist options
  • Save anonymous/2538c6e5af0ee4f52f2194cc4f0bfe7c to your computer and use it in GitHub Desktop.
Save anonymous/2538c6e5af0ee4f52f2194cc4f0bfe7c to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 8, 2016.
    92 changes: 92 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    <script src="https://fb.me/react-with-addons-15.1.0.js"></script>
    <script src="https://fb.me/react-dom-15.1.0.js"></script>
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="description" content="Building the observable pattern">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>
    </head>
    <body>

    <script id="jsbin-javascript">
    'use strict';

    function Observable() {
    this.observers = [];
    }

    Observable.prototype.add = function (observer) {
    this.observers.push(observer);
    };

    Observable.prototype.remove = function (observer) {
    var index = this.observers.indexOf(observer);
    this.observers.splice(index, 1);
    };

    Observable.prototype.notify = function (message) {
    this.observers.forEach(function (observer) {
    observer.update(message);
    });
    };

    var observer1 = {
    update: function update(message) {
    console.log('Observer 1 got:', message);
    }
    };

    var observer2 = {
    update: function update(message) {
    console.log('Observer 2 got:', message);
    }
    };

    var notifier = new Observable();
    notifier.add(observer1);
    notifier.add(observer2);
    notifier.notify('I am the message');
    </script>



    <script id="jsbin-source-javascript" type="text/javascript">function Observable() {
    this.observers = []
    }

    Observable.prototype.add = function(observer) {
    this.observers.push(observer)
    }

    Observable.prototype.remove = function(observer) {
    var index = this.observers.indexOf(observer)
    this.observers.splice(index, 1)
    }

    Observable.prototype.notify = function(message) {
    this.observers.forEach( function(observer) {
    observer.update(message)
    })
    }

    const observer1 = {
    update: function(message) {
    console.log('Observer 1 got:', message)
    }
    }

    const observer2 = {
    update: function(message) {
    console.log('Observer 2 got:', message)
    }
    }

    var notifier = new Observable()
    notifier.add(observer1)
    notifier.add(observer2)
    notifier.notify('I am the message')

    </script></body>
    </html>
    37 changes: 37 additions & 0 deletions jsbin.pewojab.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    'use strict';

    function Observable() {
    this.observers = [];
    }

    Observable.prototype.add = function (observer) {
    this.observers.push(observer);
    };

    Observable.prototype.remove = function (observer) {
    var index = this.observers.indexOf(observer);
    this.observers.splice(index, 1);
    };

    Observable.prototype.notify = function (message) {
    this.observers.forEach(function (observer) {
    observer.update(message);
    });
    };

    var observer1 = {
    update: function update(message) {
    console.log('Observer 1 got:', message);
    }
    };

    var observer2 = {
    update: function update(message) {
    console.log('Observer 2 got:', message);
    }
    };

    var notifier = new Observable();
    notifier.add(observer1);
    notifier.add(observer2);
    notifier.notify('I am the message');