PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/static/js/models/websearchmodel.js

https://bitbucket.org/gordonbrander/accordion-drawer-prototypes
JavaScript | 82 lines | 66 code | 10 blank | 6 comment | 6 complexity | 3639b3f5c1f8b8c4badcea5b9db7bd95 MD5 | raw file
Possible License(s): Apache-2.0
  1. define([
  2. 'config',
  3. 'config/searchProviders',
  4. 'underscore',
  5. 'backbone',
  6. 'models/stackmodel',
  7. 'lib/template',
  8. 'lib/urlhelper',
  9. 'logger'
  10. ], function (config, searchProviders, util, Backbone, StackModel, template, URLHelper, logger) {
  11. var WebSearchModel = Backbone.Model.extend({
  12. urlTemplate: '',
  13. initialize: function (attributes, options) {
  14. options = options || {};
  15. if (!this.get('search_provider')) this.set(
  16. { search_provider: searchProviders['default']},
  17. { silent: true }
  18. );
  19. if (!this.urlHelper) this.urlHelper = new URLHelper({
  20. template: '{ base }search/{ provider }{ query }',
  21. tokens: {
  22. base: config.applicationRoot,
  23. provider: this.get('search_provider')
  24. },
  25. query: {
  26. q: this.get('search_terms')
  27. }
  28. });
  29. this.urlTemplate = template(options.template || this.urlTemplate);
  30. },
  31. url: function() {
  32. // Make sure URL is up-to-date with current variables on model.
  33. var provider = { provider: this.get('search_provider') };
  34. var query = { q: this.get('search_terms') };
  35. return this.urlHelper.url(provider, query);
  36. },
  37. getResultModelFromElement: function(el) {
  38. var terms = this.get('search_terms');
  39. var model = new StackModel({
  40. 'place_title': el.getAttribute('data-title') || "Missing title",
  41. 'place_url': el.getAttribute('data-url') || el.href,
  42. 'search_terms': terms,
  43. // TODO: GB: hard-coded. This should be pulled from a list of registered
  44. // search providers -- probably from an API somewhere. The client should
  45. // be dumb.
  46. 'search_provider': 'bing',
  47. search_url: template(config.searchResults)({
  48. query: terms,
  49. provider: 'bing'
  50. })
  51. });
  52. // Set URL root on this model -- it's an orphan (doesn't belong)
  53. // to a collection, so we need to tell Backbone what to hit.
  54. this.urlHelper = new URLHelper({
  55. template: config.latticeUrl + '{ query }',
  56. tokens: {
  57. latticeRoot: config.latticeRoot,
  58. username: config.username,
  59. service: 'stack',
  60. method: 'search'
  61. },
  62. query: {
  63. v: 2,
  64. q: terms
  65. }
  66. });
  67. model.urlRoot = this.urlHelper.url();
  68. return model;
  69. }
  70. });
  71. return WebSearchModel;
  72. });