/js/contacts.js

https://github.com/samflores/MVC.js · JavaScript · 157 lines · 131 code · 26 blank · 0 comment · 27 complexity · b716f3e57430fdd47c718bef10217afe MD5 · raw file

  1. $(function() {
  2. window.Contact = Backbone.Model.extend({
  3. defaults: {
  4. "name": null,
  5. "phone": null,
  6. "mail": null,
  7. "twitter": null
  8. },
  9. validate: function(attrs) {
  10. if (attrs.name == null || attrs.name == "" ||
  11. ((attrs.phone == null || attrs.phone == "") &&
  12. (attrs.mail == null || attrs.mail == "") &&
  13. (attrs.twitter == null || attrs.twitter == "")))
  14. {
  15. return "Você deve preencher o <strong>nome</strong> e <strong>pelo menos uma</strong> forma de contato";
  16. }
  17. var sameName = this.collection.select(function (contact) {
  18. return contact.get("name") == attrs.name && contact.get("id") != attrs.id;
  19. });
  20. if (sameName.length > 0)
  21. return "Você já cadastrou um contato com o nome <strong>" + attrs.name + "</string>" ;
  22. }
  23. });
  24. window.ContactsList = Backbone.Collection.extend({
  25. model: Contact,
  26. localStorage: new Store("contacts")
  27. });
  28. window.Contacts = new ContactsList();
  29. window.ContactView = Backbone.View.extend({
  30. tagName: "li",
  31. template: _.template($("#contact_template").html()),
  32. events: {
  33. "click a.delete": "deleteContact",
  34. "click a.edit": "editContact"
  35. },
  36. initialize: function() {
  37. this.model.bind('change', this.render, this);
  38. this.model.bind('destroy', this.remove, this);
  39. },
  40. render: function() {
  41. $(this.el).html(this.template(this.model.toJSON()));
  42. return this;
  43. },
  44. deleteContact: function(ev) {
  45. this.model.destroy();
  46. return false;
  47. },
  48. editContact: function(ev) {
  49. this.model.trigger("edit", this.model);
  50. return false;
  51. },
  52. remove: function() {
  53. $(this.el).remove();
  54. }
  55. });
  56. window.ContactApp = Backbone.View.extend({
  57. el: $("#contacts_app"),
  58. events: {
  59. "keypress .new_contact input": "createOnEnter",
  60. "click .alert-message .close": "hideAlert"
  61. },
  62. initialize: function() {
  63. Contacts.bind("add", this.addContact, this);
  64. Contacts.bind("reset", this.addAllContacts, this)
  65. Contacts.bind("destroy", this.removeContact, this);
  66. Contacts.bind("edit", this.editContact, this);
  67. Contacts.fetch();
  68. },
  69. render: function() {
  70. return this;
  71. },
  72. addContact: function(contact) {
  73. var view = new ContactView({model: contact});
  74. this.$("#contacts_list").append(view.render().el);
  75. },
  76. addAllContacts: function() {
  77. Contacts.each(this.addContact);
  78. },
  79. editContact: function(contact) {
  80. $("#contact_id").val(contact.get("id"));
  81. $("#contact_name").val(contact.get("name"));
  82. $("#contact_phone").val(contact.get("phone"));
  83. $("#contact_mail").val(contact.get("mail"));
  84. $("#contact_twitter").val(contact.get("twitter"));
  85. $("#contact_name").focus();
  86. },
  87. createOnEnter: function(ev) {
  88. if (ev.keyCode != 13) {
  89. this.hideAlert();
  90. return;
  91. }
  92. var attributes = { name: $("#contact_name").val(), phone: $("#contact_phone").val(), mail: $("#contact_mail").val(), twitter: $("#contact_twitter").val() };
  93. var contactId = $("#contact_id").val();
  94. if (contactId === "" || $("#contact_id").val() === null) {
  95. if (Contacts.create(attributes, {error: this.showError, success: this.clearFields})) {
  96. this.showSuccess("Contato cadastrado com sucesso");
  97. }
  98. } else {
  99. var contact = Contacts.get(contactId);
  100. attributes["id"] = contactId;
  101. if (contact.save(attributes, {error: this.showError, success: this.clearFields})) {
  102. this.showSuccess("Contato atualizado com sucesso");
  103. }
  104. }
  105. },
  106. clearFields: function(model, response) {
  107. $("#contact_id").val("");
  108. $("#contact_name").val("");
  109. $("#contact_phone").val("");
  110. $("#contact_mail").val("");
  111. $("#contact_twitter").val("");
  112. $("#contact_name").focus();
  113. },
  114. removeContact: function(contact) {
  115. this.showSuccess("Contato apagado com sucesso");
  116. },
  117. hideAlert: function() {
  118. $("#alert").hide();
  119. },
  120. showError: function(model, error) {
  121. $("#alert").html("<div class='alert-message error'><a class='close' href='#'>×</a><p>" + error + "</p></div>").show();
  122. },
  123. showWarning: function(msg) {
  124. $("#alert").html("<div class='alert-message warning'><a class='close' href='#'>×</a><p>" + msg + "</p></div>").show();
  125. },
  126. showSuccess: function(msg) {
  127. $("#alert").html("<div class='alert-message success'><a class='close' href='#'>×</a><p>" + msg + "</p></div>").show();
  128. }
  129. });
  130. window.App = new ContactApp();
  131. });