Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
feat(events): add suggestions and cursorchanged events
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Voyer committed Apr 5, 2016
1 parent 32edb43 commit 3d3233f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 18 additions & 2 deletions docs/source/javascripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@

import './responsive-navigation.js';

places({
const placesAutocomplete = places({
container: document.querySelector('#landing-demo')
}).on('change', suggestion => console.log(suggestion));
});

const events = ['change', 'suggestions', 'cursorchanged'];
events.forEach(eventName =>
placesAutocomplete.on(eventName, eventData => {
if (!Array.isArray(eventData)) {
eventData = [eventData];
}

console.log(`Algolia Places: received event **${eventName}**`);
if (typeof console.table === 'function') {
console.table(eventData);
} else {
console.log('event data:', eventData);
}
})
);
12 changes: 9 additions & 3 deletions src/places.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export default function places({
const source = (query, cb) => client
.search({query})
.then(({hits}) => hits.slice(0, 5).map(hitFormatter))
.then(hits => {console.log(hits); return hits;})
.then(suggestions => {
placesInstance.emit('suggestions', suggestions);
return suggestions;
})
.then(cb)
.catch(err => console.error(err));

Expand All @@ -49,12 +52,15 @@ export default function places({
}
);

const autocompleteEvents = ['selected', 'autocompleted'];
autocompleteEvents.forEach(eventName => {
const autocompleteChangeEvents = ['selected', 'autocompleted'];
autocompleteChangeEvents.forEach(eventName => {
autocompleteInstance.on(`autocomplete:${eventName}`, (_, suggestion) => {
placesInstance.emit('change', suggestion);
});
});
autocompleteInstance.on('autocomplete:cursorchanged', (_, suggestion) => {
placesInstance.emit('cursorchanged', suggestion);
});

const autocompleteContainer = container.parentNode;
autocompleteContainer.classList.add('algolia-places');
Expand Down

0 comments on commit 3d3233f

Please sign in to comment.