PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/public/scripts/deal.js

https://bitbucket.org/leenasn/local_biz_finder
JavaScript | 95 lines | 45 code | 22 blank | 28 comment | 0 complexity | 7e6203566abbe8af2abb48c20f89dbc1 MD5 | raw file
  1. $(function(){
  2. // Deal Model
  3. // ----------
  4. window.Deal = Backbone.Model.extend({
  5. });
  6. // Deal Collection
  7. // ---------------
  8. window.DealList = Backbone.Collection.extend({
  9. // Reference to this collection's model.
  10. model: Deal,
  11. url: '/deals.json?latitude=12.9103951&longitude=77.59139429999999'
  12. });
  13. // Create our global collection of **Deals**.
  14. window.Deals = new DealList;
  15. // Deal Item View
  16. // --------------
  17. // The DOM element for a Deal item...
  18. window.DealView = Backbone.View.extend({
  19. //... is a list tag.
  20. tagName: "div",
  21. // Cache the template function for a single item.
  22. template: _.template($('#deal-template').html()),
  23. // Re-render the contents of the todo item.
  24. render: function() {
  25. $(this.el).html(this.template(this.model.toJSON()));
  26. this.setText();
  27. return this;
  28. },
  29. // To avoid XSS (not that it would be harmful in this particular app),
  30. // we use `jQuery.text` to set the contents of the todo item.
  31. setText: function() {
  32. var bizName = this.model.get('business_name');
  33. this.$('#deal-name').text(this.model.get('name'));
  34. this.$('#business-name').text(this.model.get('business_name'));
  35. var address = this.model.get('address');
  36. var latitude = this.model.get('latitude');
  37. var longitude = this.model.get('longitude');
  38. var info = "<b>"+bizName+"</b><br/>"+address;
  39. this.$('#show-details').click(function(){showMap(latitude,longitude,info)});
  40. }
  41. });
  42. // The Application
  43. // ---------------
  44. // Our overall **AppView** is the top-level piece of UI.
  45. window.AppView = Backbone.View.extend({
  46. // Instead of generating a new element, bind to the existing skeleton of
  47. // the App already present in the HTML.
  48. el: $("#app"),
  49. // At initialization we bind to the relevant events on the `Deals`
  50. // collection, when items are added or changed. Kick things off by
  51. // loading any preexisting todos that might be saved in *localStorage*.
  52. initialize: function() {
  53. Deals.bind('reset', this.addAll, this);
  54. Deals.fetch();
  55. },
  56. // Re-rendering the App just means refreshing the statistics -- the rest
  57. // of the app doesn't change.
  58. render: function() {
  59. },
  60. // Add a single todo item to the list by creating a view for it, and
  61. // appending its element to the `<ul>`.
  62. addOne: function(deal) {
  63. var view = new DealView({model: deal});
  64. $("#deal-list").append(view.render().el);
  65. },
  66. // Add all items in the **Deals** collection at once.
  67. addAll: function() {
  68. Deals.each(this.addOne);
  69. },
  70. });
  71. // Finally, we kick things off by creating the **App**.
  72. window.App = new AppView;
  73. });