PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/duckduckhack/spice/spice_displaying.md

https://gitlab.com/Guy1394/duckduckgo-documentation
Markdown | 92 lines | 61 code | 31 blank | 0 comment | 0 complexity | 5bbd31b1593c429b754f8a426cdcc5b4 MD5 | raw file
  1. # Adding Your Spice to the DuckDuckGo AnswerBar
  2. Once your Instant Answer has been triggered, and the API request has returned a response to the client, the final step is to display your results onscreen.
  3. ![answerbar](https://images.duckduckgo.com/iu/?u=https%3A%2F%2Fraw.githubusercontent.com%2Fduckduckgo%2Fduckduckgo-documentation%2Fmaster%2Fduckduckhack%2Fassets%2Fdiagrams%2Fanswerbar.png&f=1)
  4. ## Contents of the Spice Frontend Callback
  5. The Spice frontend callback function, [covered in the Spice Basic Tutorial](https://duck.co/duckduckhack/spice_basic_tutorial#npm-spice-frontend-javascript), contains the code which displays your Spice.
  6. The most important part of this callback, and often the only part, is calling [`Spice.add()`](#codespiceaddcode-overview). This function is powerful and gives you a lot of control over your results' appearance, context, and user interactions.
  7. ## Available Options
  8. The properties you can pass to the `Spice.add()` function are documented in the [Instant Answer Display Reference](https://duck.co/duckduckhack/display_reference). This document provides an in-depth overview of all that `Spice.add()` allows you to do.
  9. In some scenarios, you may also want to handle the AnswerBar's events (for example, to stop media playing when the user hides your Instant Answer). These [events](https://duck.co/duckduckhack/display_reference#events) are covered at the end of the reference.
  10. ## Calling `Spice.add()`
  11. The following is a code summary of how the options covered in the [Instant Answer Display Reference](https://duck.co/duckduckhack/display_reference) are passed to `Spice.add()`
  12. ```javascript
  13. Spice.add({
  14. // Required properties:
  15. id: String,
  16. name: String,
  17. data: Array or Object (in the case of a single item),
  18. meta: {
  19. searchTerm: String,
  20. itemType: String,
  21. primaryText: String,
  22. secondaryText: String,
  23. sourceName: String,
  24. sourceLogo: String,
  25. sourceUrl: String (url),
  26. sourceIcon: Boolean,
  27. sourceIconUrl: String (url),
  28. snippetChars: Integer
  29. },
  30. templates: {
  31. group: String,
  32. item: String|Function,
  33. item_custom: String|Function,
  34. item_mobile: String|Function,
  35. detail: String|Function,
  36. detail_custom: String|Function,
  37. detail_mobile: String|Function,
  38. item_detail: String|Function,
  39. options: Object
  40. variants: Object
  41. elClass: Object
  42. },
  43. // Optional properties:
  44. normalize: Function,
  45. relevancy: {
  46. type: String,
  47. skip_words: [, String],
  48. primary: [, Object],
  49. dup: String,
  50. },
  51. view: String,
  52. model: String,
  53. sort_fields: Object,
  54. sort_default: String|Object,
  55. onItemSelected: Function,
  56. onItemUnselect: Function,
  57. onShow: Function,
  58. onHide: Function
  59. });
  60. ```
  61. For more information on each property and its usage, visit the [Instant Answer Display Reference](https://duck.co/duckduckhack/display_reference).