Skip to content

Instantly share code, notes, and snippets.

@benqus
Created November 11, 2014 15:47
Show Gist options
  • Select an option

  • Save benqus/9416627c20cfef93eae2 to your computer and use it in GitHub Desktop.

Select an option

Save benqus/9416627c20cfef93eae2 to your computer and use it in GitHub Desktop.

Revisions

  1. benqus created this gist Nov 11, 2014.
    16 changes: 16 additions & 0 deletions Sibling.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    function Sibling() {
    mediator.add(this);
    this.id = Sibling.getUniqueID();
    }

    Sibling.getUniqueID = (function () {
    var id = 0;

    return function () {
    return id++;
    };
    }());

    Sibling.prototype.onNotify = function () {
    console.log(this.id);
    };
    24 changes: 24 additions & 0 deletions mediator.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    var mediator = {
    siblings: [],

    notify: function () {
    var args = Array.prototype.slice.call(arguments);

    this.siblings.forEach(function (sibling) {
    sibling.onNotify.apply(sibling, args);
    });
    },

    add: function (sibling) {
    if (this.siblings.indexOf(sibling) === -1 && typeof sibling.onNotify === 'function') {
    this.siblings.push(sibling);
    }
    },

    remove: function (sibling) {
    var index = this.siblings.indexOf(sibling);
    if (index > -1) {
    this.sibling.splice(index, 1);
    }
    }
    };