Guest User

Untitled

a guest
Oct 13th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. (function ($) {
  2.  
  3. //demo data
  4. var contacts = [
  5. { name: "Contact 1", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  6. { name: "Contact 2", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  7. { name: "Contact 3", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
  8. { name: "Contact 4", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
  9. { name: "Contact 5", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  10. { name: "Contact 6", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
  11. { name: "Contact 7", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
  12. { name: "Contact 8", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" }
  13. ];
  14.  
  15. //define product model
  16. var Contact = Backbone.Model.extend({
  17. defaults: {
  18. photo: "/img/placeholder.png"
  19. }
  20. });
  21.  
  22. //define directory collection
  23. var Directory = Backbone.Collection.extend({
  24. model: Contact
  25. });
  26.  
  27. //define individual contact view
  28. var ContactView = Backbone.View.extend({
  29. tagName: "article",
  30. className: "contact-container",
  31. template: $("#contactTemplate").html(),
  32.  
  33. render: function () {
  34. var tmpl = _.template(this.template);
  35.  
  36. $(this.el).html(tmpl(this.model.toJSON()));
  37. return this;
  38. }
  39. });
  40.  
  41. //define master view
  42. var DirectoryView = Backbone.View.extend({
  43. el: $("#contacts"),
  44.  
  45. initialize: function () {
  46. this.collection = new Directory(contacts);
  47. this.render();
  48. },
  49.  
  50. render: function () {
  51. var that = this;
  52. _.each(this.collection.models, function (item) {
  53. that.renderContact(item);
  54. }, this);
  55. },
  56.  
  57. renderContact: function (item) {
  58. var contactView = new ContactView({
  59. model: item
  60. });
  61. this.$el.append(contactView.render().el);
  62. }
  63. });
  64.  
  65. //create instance of master view
  66. var directory = new DirectoryView();
  67.  
  68. } (jQuery));
Add Comment
Please, Sign In to add comment