muu

DEPRECATED lightweight JS framework
git clone https://git.ce9e.org/muu.git

commit
a79f07f5b298c38a8cab30eff7642e87989aa29e
parent
4fa1b7e67d73ae228eb925b7ea91ca1852353144
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2015-09-08 18:41
add directive.on

Diffstat

M src/muu-directive.js 11 +++++++++++
M test/test-directive.js 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 78 insertions, 0 deletions


diff --git a/src/muu-directive.js b/src/muu-directive.js

@@ -121,6 +121,17 @@ define('muu-directive', ['muu-dom-helpers', 'muu-js-helpers', 'muu-update-dom'],
  121   121         };
  122   122 
  123   123         /**
   -1   124          * @param {string} eventName
   -1   125          * @param {Function} callback
   -1   126          * @return {function()} An unregister function
   -1   127          */
   -1   128         this.on = function(eventName, fn) {
   -1   129             return $.on(root, 'muu-' + eventName, function(event) {
   -1   130                 return fn(event.detail);
   -1   131             });
   -1   132         };
   -1   133 
   -1   134         /**
  124   135          * Get all model data as a flat object.
  125   136          *
  126   137          * @return {Object.<string, string|number|boolean>}

diff --git a/test/test-directive.js b/test/test-directive.js

@@ -124,6 +124,73 @@ define(['muu-directive', 'muu-js-helpers', 'muu-dom-helpers'], function(Directiv
  124   124             });
  125   125         });
  126   126 
   -1   127         describe('on', function() {
   -1   128             var directive;
   -1   129             var button;
   -1   130 
   -1   131             beforeEach(function() {
   -1   132                 var element = document.createElement('div');
   -1   133                 var template = '<a class="button" data-onclick="test"></a>';
   -1   134                 directive = new Directive(element, template, registry);
   -1   135                 directive.update({});
   -1   136                 button = element.querySelector('.button');
   -1   137             });
   -1   138 
   -1   139             it('registers an event listener for an event alias', function() {
   -1   140                 var spy = sinon.spy();
   -1   141                 directive.on('test', spy);
   -1   142 
   -1   143                 expect(spy.callCount).to.equal(0);
   -1   144                 button.dispatchEvent($.createEvent('click'));
   -1   145                 expect(spy.callCount).to.equal(1);
   -1   146                 button.dispatchEvent($.createEvent('click'));
   -1   147                 expect(spy.callCount).to.equal(2);
   -1   148                 button.dispatchEvent($.createEvent('click'));
   -1   149                 expect(spy.callCount).to.equal(3);
   -1   150             });
   -1   151             it('calls callback with original event', function() {
   -1   152                 var spy = sinon.spy();
   -1   153                 directive.on('test', spy);
   -1   154 
   -1   155                 button.dispatchEvent($.createEvent('click', undefined, undefined, 'asd'));
   -1   156                 expect(spy.firstCall.args[0].detail).to.equal('asd');
   -1   157             });
   -1   158             it('can register more than one event listener', function() {
   -1   159                 var spy1 = sinon.spy();
   -1   160                 directive.on('test', spy1);
   -1   161 
   -1   162                 expect(spy1.callCount).to.equal(0);
   -1   163                 button.dispatchEvent($.createEvent('click'));
   -1   164                 expect(spy1.callCount).to.equal(1);
   -1   165 
   -1   166                 var spy2 = sinon.spy();
   -1   167                 directive.on('test', spy2);
   -1   168 
   -1   169                 expect(spy2.callCount).to.equal(0);
   -1   170                 button.dispatchEvent($.createEvent('click'));
   -1   171                 expect(spy1.callCount).to.equal(2);
   -1   172                 expect(spy2.callCount).to.equal(1);
   -1   173                 button.dispatchEvent($.createEvent('click'));
   -1   174                 expect(spy1.callCount).to.equal(3);
   -1   175                 expect(spy2.callCount).to.equal(2);
   -1   176             });
   -1   177             it('returns an unregister function', function() {
   -1   178                 var spy = sinon.spy();
   -1   179                 var unregister = directive.on('test', spy);
   -1   180 
   -1   181                 expect(spy.callCount).to.equal(0);
   -1   182                 button.dispatchEvent($.createEvent('click'));
   -1   183                 expect(spy.callCount).to.equal(1);
   -1   184 
   -1   185                 unregister();
   -1   186 
   -1   187                 button.dispatchEvent($.createEvent('click'));
   -1   188                 expect(spy.callCount).to.equal(1);
   -1   189                 button.dispatchEvent($.createEvent('click'));
   -1   190                 expect(spy.callCount).to.equal(1);
   -1   191             });
   -1   192         });
   -1   193 
  127   194         describe('getModel', function() {
  128   195             var directive;
  129   196