Skip to content

Commit 4de8558

Browse files
authored
Merge pull request #65 from Tol1/fix-custom-event-args
Fix custom event arguments
2 parents cee60a9 + e7fb159 commit 4de8558

File tree

8 files changed

+94
-46
lines changed

8 files changed

+94
-46
lines changed

doc/jquery_typeahead.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,19 @@ a typeahead.
212212
occurred in.
213213

214214
* `typeahead:select` – Fired when a suggestion is selected. The event handler
215-
will be invoked with 2 arguments: the jQuery event object and the suggestion
216-
object that was selected.
215+
will be invoked with 3 arguments: the jQuery event object, the suggestion
216+
object that was selected, and the name of the dataset the suggestion belongs
217+
to.
217218

218-
* `typeahead:autocomplete` – Fired when a autocompletion occurs. The
219-
event handler will be invoked with 2 arguments: the jQuery event object and
220-
the suggestion object that was used for autocompletion.
219+
* `typeahead:autocomplete` – Fired when a autocompletion occurs. The event
220+
handler will be invoked with 3 arguments: the jQuery event object, the
221+
suggestion object that was used for autocompletion, and the name of the
222+
dataset the suggestion belongs to.
221223

222224
* `typeahead:cursorchange` – Fired when the results container cursor moves. The
223-
event handler will be invoked with 2 arguments: the jQuery event object and
224-
the suggestion object that was moved to.
225+
event handler will be invoked with 3 arguments: the jQuery event object, the
226+
suggestion object that was moved to, and the name of the dataset the
227+
suggestion belongs to.
225228

226229
* `typeahead:asyncrequest` – Fired when an async request for suggestions is
227230
sent. The event handler will be invoked with 3 arguments: the jQuery event

src/typeahead/dataset.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var Dataset = (function() {
1010
var keys, nameGenerator;
1111

1212
keys = {
13+
dataset: 'tt-selectable-dataset',
1314
val: 'tt-selectable-display',
1415
obj: 'tt-selectable-object'
1516
};
@@ -41,7 +42,7 @@ var Dataset = (function() {
4142
www.mixin(this);
4243

4344
this.highlight = !!o.highlight;
44-
this.name = o.name || nameGenerator();
45+
this.name = _.toStr(o.name || nameGenerator());
4546

4647
this.limit = o.limit || 5;
4748
this.displayFn = getDisplayFn(o.display || o.displayKey);
@@ -70,6 +71,7 @@ var Dataset = (function() {
7071

7172
if ($el.data(keys.obj)) {
7273
return {
74+
dataset: $el.data(keys.dataset) || '',
7375
val: $el.data(keys.val) || '',
7476
obj: $el.data(keys.obj) || null
7577
};
@@ -108,7 +110,7 @@ var Dataset = (function() {
108110
this._empty();
109111
}
110112

111-
this.trigger('rendered', this.name, suggestions, false);
113+
this.trigger('rendered', suggestions, false, this.name);
112114
},
113115

114116
_append: function append(query, suggestions) {
@@ -129,7 +131,7 @@ var Dataset = (function() {
129131
this._renderNotFound(query);
130132
}
131133

132-
this.trigger('rendered', this.name, suggestions, true);
134+
this.trigger('rendered', suggestions, true, this.name);
133135
},
134136

135137
_renderSuggestions: function renderSuggestions(query, suggestions) {
@@ -189,6 +191,7 @@ var Dataset = (function() {
189191
context = that._injectQuery(query, suggestion);
190192

191193
$el = $(that.templates.suggestion(context))
194+
.data(keys.dataset, that.name)
192195
.data(keys.obj, suggestion)
193196
.data(keys.val, that.displayFn(suggestion))
194197
.addClass(that.classes.suggestion + ' ' + that.classes.selectable);
@@ -242,7 +245,7 @@ var Dataset = (function() {
242245
this.cancel = function cancel() {
243246
canceled = true;
244247
that.cancel = $.noop;
245-
that.async && that.trigger('asyncCanceled', query);
248+
that.async && that.trigger('asyncCanceled', query, that.name);
246249
};
247250

248251
this.source(query, sync, async);
@@ -258,7 +261,7 @@ var Dataset = (function() {
258261
that._overwrite(query, suggestions);
259262

260263
if (rendered < that.limit && that.async) {
261-
that.trigger('asyncRequested', query);
264+
that.trigger('asyncRequested', query, that.name);
262265
}
263266
}
264267

@@ -272,7 +275,7 @@ var Dataset = (function() {
272275
var idx = Math.abs(rendered - that.limit);
273276
rendered += idx;
274277
that._append(query, suggestions.slice(0, idx));
275-
that.async && that.trigger('asyncReceived', query);
278+
that.async && that.trigger('asyncReceived', query, that.name);
276279
}
277280
}
278281
},

src/typeahead/event_bus.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ var EventBus = (function() {
4141
// ### private
4242

4343
_trigger: function(type, args) {
44-
var $e;
44+
var $e = $.Event(namespace + type);
4545

46-
$e = $.Event(namespace + type);
47-
(args = args || []).unshift($e);
48-
49-
this.$el.trigger.apply(this.$el, args);
46+
this.$el.trigger.call(this.$el, $e, args || []);
5047

5148
return $e;
5249
},

src/typeahead/typeahead.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ var Typeahead = (function() {
131131
this._updateHint();
132132
},
133133

134-
_onDatasetRendered: function onDatasetRendered(type, dataset, suggestions, async) {
134+
_onDatasetRendered: function onDatasetRendered(type, suggestions, async, dataset) {
135135
this._updateHint();
136136
this.eventBus.trigger('render', suggestions, async, dataset);
137137
},
@@ -347,10 +347,10 @@ var Typeahead = (function() {
347347
select: function select($selectable) {
348348
var data = this.menu.getSelectableData($selectable);
349349

350-
if (data && !this.eventBus.before('select', data.obj)) {
350+
if (data && !this.eventBus.before('select', data.obj, data.dataset)) {
351351
this.input.setQuery(data.val, true);
352352

353-
this.eventBus.trigger('select', data.obj);
353+
this.eventBus.trigger('select', data.obj, data.dataset);
354354
this.close();
355355

356356
// return true if selection succeeded
@@ -367,9 +367,9 @@ var Typeahead = (function() {
367367
data = this.menu.getSelectableData($selectable);
368368
isValid = data && query !== data.val;
369369

370-
if (isValid && !this.eventBus.before('autocomplete', data.obj)) {
370+
if (isValid && !this.eventBus.before('autocomplete', data.obj, data.dataset)) {
371371
this.input.setQuery(data.val);
372-
this.eventBus.trigger('autocomplete', data.obj);
372+
this.eventBus.trigger('autocomplete', data.obj, data.dataset);
373373

374374
// return true if autocompletion succeeded
375375
return true;
@@ -379,18 +379,20 @@ var Typeahead = (function() {
379379
},
380380

381381
moveCursor: function moveCursor(delta) {
382-
var query, $candidate, data, payload, cancelMove;
382+
var query, $candidate, data, suggestion, datasetName, cancelMove;
383383

384384
query = this.input.getQuery();
385+
385386
$candidate = this.menu.selectableRelativeToCursor(delta);
386387
data = this.menu.getSelectableData($candidate);
387-
payload = data ? data.obj : null;
388+
suggestion = data ? data.obj : null;
389+
datasetName = data ? data.dataset : null;
388390

389391
// update will return true when it's a new query and new suggestions
390392
// need to be fetched – in this case we don't want to move the cursor
391393
cancelMove = this._minLengthMet() && this.menu.update(query);
392394

393-
if (!cancelMove && !this.eventBus.before('cursorchange', payload)) {
395+
if (!cancelMove && !this.eventBus.before('cursorchange', suggestion, datasetName)) {
394396
this.menu.setCursor($candidate);
395397

396398
// cursor moved to different selectable
@@ -404,7 +406,7 @@ var Typeahead = (function() {
404406
this._updateHint();
405407
}
406408

407-
this.eventBus.trigger('cursorchange', payload);
409+
this.eventBus.trigger('cursorchange', suggestion, datasetName);
408410

409411
// return true if move succeeded
410412
return true;

test/typeahead/dataset_spec.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('Dataset', function() {
2-
var www = WWW(), mockSuggestions, mockSuggestionsDisplayFn;
2+
var www = WWW(), mockSuggestions, mockSuggestionsDisplayFn, mockAsyncSuggestions;
33

44
mockSuggestions = [
55
{ value: 'one', raw: { value: 'one' } },
@@ -13,6 +13,14 @@ describe('Dataset', function() {
1313
{ display: '6' }
1414
];
1515

16+
mockAsyncSuggestions = [
17+
{ value: 'four', raw: { value: 'four' } },
18+
{ value: 'five', raw: { value: 'five' } },
19+
{ value: 'six', raw: { value: 'six' } },
20+
{ value: 'seven', raw: { value: 'seven' } },
21+
{ value: 'eight', raw: { value: 'eight' } },
22+
];
23+
1624
beforeEach(function() {
1725
this.dataset = new Dataset({
1826
name: 'test',
@@ -112,7 +120,7 @@ describe('Dataset', function() {
112120

113121
this.dataset.update('woah');
114122

115-
expect(spy).toHaveBeenCalled();
123+
expect(spy).toHaveBeenCalledWith('asyncRequested', 'woah', 'test');
116124
});
117125

118126
it('should not trigger asyncRequested when not expecting backfill', function() {
@@ -153,7 +161,7 @@ describe('Dataset', function() {
153161
waits(100);
154162

155163
runs(function() {
156-
expect(spy).toHaveBeenCalled();
164+
expect(spy).toHaveBeenCalledWith('asyncCanceled', 'woah', 'test');
157165
});
158166
});
159167

@@ -186,7 +194,7 @@ describe('Dataset', function() {
186194
waits(100);
187195

188196
runs(function() {
189-
expect(spy).toHaveBeenCalled();
197+
expect(spy).toHaveBeenCalledWith('asyncReceived', 'woah', 'test');
190198
});
191199
});
192200

@@ -379,7 +387,8 @@ describe('Dataset', function() {
379387
});
380388
});
381389

382-
it('should trigger rendered after suggestions are rendered', function() {
390+
391+
it('should trigger rendered after sync suggestions are rendered', function() {
383392
var spy;
384393

385394
this.dataset.onSync('rendered', spy = jasmine.createSpy());
@@ -388,6 +397,27 @@ describe('Dataset', function() {
388397
this.dataset.update('woah');
389398

390399
waitsFor(function() { return spy.callCount; });
400+
401+
runs(function() {
402+
expect(spy).toHaveBeenCalledWith('rendered', mockSuggestions, false, 'test');
403+
});
404+
});
405+
406+
it('should trigger rendered after suggestions are rendered', function() {
407+
var spy;
408+
409+
this.dataset.async = true;
410+
this.source.andCallFake(fakeGetWithAsyncSuggestions);
411+
412+
this.dataset.onSync('rendered', spy = jasmine.createSpy());
413+
this.dataset.update('woah');
414+
415+
waitsFor(function() { return spy.callCount === 2; });
416+
417+
runs(function() {
418+
expect(spy).toHaveBeenCalledWith('rendered', mockSuggestions, false, 'test');
419+
expect(spy).toHaveBeenCalledWith('rendered', mockAsyncSuggestions.slice(0, 2), true, 'test');
420+
});
391421
});
392422
});
393423

test/typeahead/event_bus_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('EventBus', function() {
1616

1717
this.$el.on('typeahead:fiz', spy);
1818

19-
this.eventBus.trigger('fiz');
20-
expect(spy).toHaveBeenCalled();
19+
this.eventBus.trigger('fiz', 'foo', 'bar');
20+
expect(spy).toHaveBeenCalledWith(jasmine.any(Object), 'foo', 'bar');
2121
});
2222

2323
it('#before should return false if default was not prevented', function() {

test/typeahead/menu_spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,14 @@ describe('Menu', function() {
264264
var $selectable, datum;
265265

266266
$selectable = $('<div>').data({
267+
'tt-selectable-dataset': 'foo',
267268
'tt-selectable-display': 'one',
268269
'tt-selectable-object': 'two'
269270
});
270271

271272
data = this.view.getSelectableData($selectable);
272273

273-
expect(data).toEqual({ val: 'one', obj: 'two' });
274+
expect(data).toEqual({ dataset: 'foo', val: 'one', obj: 'two' });
274275
});
275276

276277
it('should return null if no element is given', function() {

0 commit comments

Comments
 (0)