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

/web/global/js/modules/ingredients.js

https://bitbucket.org/trentonstrong/beautysafe-web
JavaScript | 88 lines | 70 code | 15 blank | 3 comment | 3 complexity | de35ebe1d910a471ce2532a1a0db9908 MD5 | raw file
  1. /* Author:
  2. */
  3. var bs = bs = window.bs || {};
  4. bs.Ingredient = Backbone.Model.extend({});
  5. bs.IngredientSearchResultsCollection = Backbone.Collection.extend({
  6. url: '/api/ingredients/search/',
  7. model: bs.Ingredient
  8. });
  9. bs.IngredientSearchResultsView = Backbone.View.extend({
  10. tagName: 'div',
  11. className: 'search-results span4',
  12. events: {
  13. 'reset': 'render'
  14. },
  15. initialize: function() {
  16. _.bindAll(this);
  17. if (!this.options.collection) {
  18. throw 'Must define collection!';
  19. }
  20. this.collection.bind('reset', this.render);
  21. this.template = _.template($('#ingredient_results').html());
  22. },
  23. render: function() {
  24. $(this.el).html(this.template({
  25. results: this.collection.toJSON()
  26. }));
  27. $(this.el).find('li').click(function (event) {
  28. $(this).find('dl').toggle();
  29. });
  30. return this;
  31. }
  32. });
  33. bs.IngredientSearchView = Backbone.View.extend({
  34. events: {
  35. 'click button.search': 'doSearch',
  36. 'click button.reset': 'clearResults'
  37. },
  38. initialize: function() {
  39. _.bindAll(this);
  40. this.results = new bs.IngredientSearchResultsCollection();
  41. this.resultsView = new bs.IngredientSearchResultsView({
  42. collection: this.results
  43. });
  44. this.searchResultsEl = $(this.resultsView.render().el).hide();
  45. this.el.append(this.searchResultsEl);
  46. this.textinput = this.$('textarea.input');
  47. },
  48. doSearch: function() {
  49. var query = $.trim(this.textinput.val());
  50. if (query === "") {
  51. return;
  52. }
  53. var searchCallback = _.bind(function() {
  54. this.searchResultsEl.show();
  55. this.$('button.reset').show();
  56. }, this);
  57. this.results.fetch({
  58. data: {
  59. q: query
  60. },
  61. success: searchCallback });
  62. },
  63. clearResults: function() {
  64. this.searchResultsEl.hide();
  65. this.textinput.val("");
  66. }
  67. });
  68. $(document).ready(function() {
  69. var searchView = new bs.IngredientSearchView({
  70. el: $('#ingredient_search')
  71. });
  72. });