xmosaicsource: rename event to a more specific one + enable bubbling

authorSamuel Trégouët <samuel.tregouet@logilab.fr>
changesetbaaa771c97f4
branchdefault
phasepublic
hiddenno
parent revision#a13d46e7ef7b xmosaicsource: move sources management from xmosaicframe to xmosaicsource
child revision#bc2174cd99ff xmosaicframe: always show result view
files modified by this revision
components/xmosaicsource/xmosaicsource.js
test/src/xmosaicsource.js
# HG changeset patch
# User Samuel Trégouët <samuel.tregouet@logilab.fr>
# Date 1429003205 -7200
# Tue Apr 14 11:20:05 2015 +0200
# Node ID baaa771c97f4619469ad29303425c46a9299547b
# Parent a13d46e7ef7bdaab9593e1640e8de63d50ebad42
xmosaicsource: rename event to a more specific one + enable bubbling

diff --git a/components/xmosaicsource/xmosaicsource.js b/components/xmosaicsource/xmosaicsource.js
@@ -73,30 +73,33 @@
1              fetcher = cwFetch;
2          } else if (source === 'http') {
3              fetcher = httpFetch;
4          }
5          if (!fetcher) {
6 -            this.dispatchEvent(new CustomEvent('error', {
7 +            this.dispatchEvent(new CustomEvent('source-error', {
8 +                bubbles: true,
9                  detail: {
10                      err: new Error('nothing to fetch: ' +
11                                     'unknown source "' + source + '"')
12                  }
13              }));
14              return;
15          }
16          fetcher(this.dataset.baseurl, this.dataset.query)
17              .done(function(data, status, xhr) {
18 -                this.dispatchEvent(new CustomEvent('load', {
19 +                this.dispatchEvent(new CustomEvent('source-load', {
20 +                    bubbles: true,
21                      detail: {
22                          rset: data,
23                          xhr: xhr,
24                          reqUrl: xhr.reqUrl
25                      }
26                  }));
27              }.bind(this))
28              .fail(function(xhr, err, msg) {
29 -                this.dispatchEvent(new CustomEvent('error', {
30 +                this.dispatchEvent(new CustomEvent('source-error', {
31 +                    bubbles: true,
32                      detail: {
33                          err: err,
34                          msg: msg,
35                          xhr: xhr
36                      }
@@ -121,10 +124,11 @@
37              ev.preventDefault();
38              if (dataset.autohide) {
39                  this.hideForm();
40              }
41          }.bind(this));
42 +
43      };
44 
45      xSourceProto.displayForm = function displayForm() {
46          this.formNode.style.display = 'block';
47      };
diff --git a/test/src/xmosaicsource.js b/test/src/xmosaicsource.js
@@ -19,21 +19,29 @@
48          // restore ajax
49          server.restore();
50      });
51 
52      it('should fire `error` CustomEvent after creation', function(done) {
53 -        sourceElement.addEventListener('error', function(ev) {
54 +        sourceElement.addEventListener('source-error', function(ev) {
55              expect(ev.detail).to.have.property('err');
56              expect(ev.detail.err).to.be.an.instanceOf(Error);
57              done();
58          });
59          document.body.appendChild(sourceElement);
60 
61      });
62 
63 +    it('should fire bubble enabled event', function(done) {
64 +        sourceElement.addEventListener('source-error', function(ev) {
65 +            expect(ev.bubbles).to.equal(true);
66 +            done();
67 +        });
68 +        document.body.appendChild(sourceElement);
69 +    });
70 +
71      it('should fetch cw source', function(done) {
72 -        sourceElement.addEventListener('load', function(ev) {
73 +        sourceElement.addEventListener('source-load', function(ev) {
74              expect(ev.detail).to.have.property('xhr');
75              expect(ev.detail).to.have.property('rset');
76              expect(ev.detail).to.have.property('reqUrl');
77              expect(ev.detail.rset).to.deep.equal([]);
78              done();