Skip to content

Commit

Permalink
Add item data to awesomplete-select event
Browse files Browse the repository at this point in the history
  • Loading branch information
vlazar committed Jan 30, 2016
1 parent dd4db90 commit da7573c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ _.prototype = {
if (selected) {
var allowed = $.fire(this.input, "awesomplete-select", {
text: selected.textContent,
data: this._suggestions[elementSiblingIndex(selected)],
origin: origin || selected
});

Expand All @@ -209,15 +210,15 @@ _.prototype = {
// Populate list with options that match
this.ul.innerHTML = "";

this._list
this._suggestions = this._list
.filter(function(item) {
return me.filter(item, value);
})
.sort(this.sort)
.every(function(text, i) {
me.ul.appendChild(me.item(text, value));
.slice(0, this.maxItems);

return i < me.maxItems - 1;
this._suggestions.forEach(function(text) {
me.ul.appendChild(me.item(text, value));
});

if (this.ul.children.length === 0) {
Expand Down Expand Up @@ -290,6 +291,12 @@ function configure(instance, properties, o) {
}
}

function elementSiblingIndex(el) {
/* eslint-disable no-cond-assign */
for (var i = 0; el = el.previousElementSibling; i++);
return i;
}

// Helpers

var slice = Array.prototype.slice;
Expand Down
1 change: 1 addition & 0 deletions test/api/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe("awesomplete.select", function () {
expect(handler).toHaveBeenCalledWith(
jasmine.objectContaining({
text: expectedTxt,
data: expectedTxt,
origin: this.selectArgument || this.subject.ul.children[0]
})
);
Expand Down

0 comments on commit da7573c

Please sign in to comment.