Backbone.js Series Tutorial 13: Creating a simple Contact application

Source: Internet
Author: User
Tags add button type extend split trim

Overview

Imagine that you are using a spreadsheet tool. First create a form called a contact (that is, a collection), and then define the data field headings in the form, and enter "first name", "Last Name", and "phone" (a model) in the title. Basically, you already have an electronic form to keep contact data ({firstName: "Jane, LastName:" Doe ", Phone:" 111-111-1111 "}). Now you want to use this information from the form to create a UI (a view) to view/Add/Remove Contacts. Backbone can do it! Let's get started!

Inherit Backbone.model, create the contact function/class

 
  
  
  1. var contact = Backbone.Model.extend ({
  2. Defaults: {
  3. Firstname:null,
  4. Lastname:null,
  5. Phone:null
  6. },
  7. Getfullname:function () {
  8. Return This.get ("firstName") + "" + This.get ("LastName");
  9. }
  10. });

Instantiate the Contacts collection, passing a model constructor/category to a contact

 
  
  
  1. var contacts = new Backbone.collection ({//Add some data for it
  2. FirstName: "Jane",
  3. LastName: "Doe",
  4. Phone: "111-111-1111"
  5. }, {
  6. Model:contact
  7. });

Inherit Backbone.view, create Addcontactsview, instantiate an instance

 
 
  1. var Contactlistview = Backbone.View.extend ({
  2. El: "#contacts",
  3. Events: {
  4. "Click Li button": "Removecontact"
  5. },
  6. Initialize:function () {
  7. This.render (); Render List
  8. This.listento (Contacts, "Add Remove", this.render); The Magic
  9. },
  10. Removecontact:function (e) {
  11. $ (e.target). Parent ("Li"). Remove ();
  12. Contacts.findwhere ({
  13. FirstName: $ (e.target). Parent ("Li"). Find ("span"). Text (). Split ("") [0].trim (),
  14. LastName: $ (e.target). Parent ("Li"). Find ("span"). Text (). Split ("") [1].trim ()
  15. }). Destroy (); Internal "Remove" event will be invoked
  16. },
  17. Render:function () {
  18. if (Contacts.length > 0) {
  19. this. $el. empty ();
  20. Contacts.each (function (contact) {
  21. this. $el. Append ("<li><span>" + contact.getfullname () + "</span>" + "/" + contact.get ("phone") + "< Button type= "button" class= "Btn-xs btn-danger removecontactbtn" (">X</button></li>");
  22. }, this);
  23. }
  24. }
  25. });
  26. var contactlistviewinstance = new Contactlistview ();

Inherit Backbone.view, create Contactlistview, instantiate an instance

 
 
    1. var Addcontactsview = Backbone.View.extend ({
    2. El: "FieldSet",
    3. Events: {
    4. "Click button": "Addcontact"
    5. },
    6. Addcontact:function () {
    7. var firstName = this.$ ("#firstName"). Val ();
    8. var lastName = this.$ ("#lastName"). Val ();
    9. var phone = this.$ ("#phone"). Val ();
    10. if (firstName && lastName && phone) {
    11. Contacts.add ({//will invoke backbone internal "add" event
    12. Firstname:firstname,
    13. Lastname:lastname,
    14. Phone:phone
    15. });
    16. this.$ ("Input"). Val ("");
    17. }
    18. }
    19. });
    20. var addcontactsviewinstance = new Addcontactsview ();

Example demonstration

The following is a demonstration of this section's work on the contact application. Review the application code until you fully understand the roles and functions of each part of the creation process (i.e. model, collection, view). You need to refer to the contents of the first few sections.

Conclusion

This article guides the reader through a thorough understanding of backbone views, models, and collections. In the next section of this article, we will try to synchronize and associate the methods of the synchronization model with the collection to the background.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.