Advertisement
Guest User

Untitled

a guest
Jan 20th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.    
  3.     /*
  4.     _.templateSettings = {
  5.        // interpolate: /\{\{(.+?)\}\}/g   // {{ }} syntax for templating insteat of  of <%= property %>
  6.     };
  7.     */
  8.  
  9.     var contacts = [
  10.         { name: "Contact 1", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  11.         { name: "Contact 2", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  12.         { name: "Contact 3", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
  13.         { name: "Contact 4", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
  14.         { name: "Contact 5", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" },
  15.         { name: "Contact 6", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "colleague" },
  16.         { name: "Contact 7", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "friend" },
  17.         { name: "Contact 8", address: "1, a street, a town, a city, AB12 3CD", tel: "0123456789", email: "anemail@me.com", type: "family" }
  18.     ];
  19.  
  20.     // A model represents the data of an application; in our application this will be an individual contact, which will have attributes such as a name, a contact number, etc. You could say that an individual model represents the atomic part of the application – the smallest possible unit of functionality.
  21.  
  22.     var Contact = Backbone.Model.extend({
  23.         defaults: {
  24.             photo: "img/placeholder.png",
  25.             name: "",
  26.             address: "",
  27.             tel: "",
  28.             email: "",
  29.             type: ""
  30.         }
  31.     });
  32.  
  33.     // A collection is a class for managing groups of models.
  34.     var Directory = Backbone.Collection.extend({
  35.         model: Contact
  36.     });
  37.  
  38.  
  39.     var ContactView = Backbone.View.extend({
  40.         tagName: "article",                     // tagname and classname I don't see where are used
  41.         className: "contact-container",
  42.         template: $("#contactTemplate").html(),
  43.         editTemplate: _.template($("#contactEditTemplate").html()),
  44.         render: function () {
  45.             var tmpl = _.template(this.template);
  46.  
  47.             //console.log(this.model.toJSON());
  48.            
  49.             this.$el.html(tmpl(this.model.toJSON()));
  50.             return this;
  51.         },/*
  52.         events: {
  53.             "click button.delete": "deleteContact",
  54.             "click button.edit": "editContact",
  55.             "change select.type": "addType",
  56.             "click button.save": "saveEdits",
  57.             "click button.cancel": "cancelEdit"
  58.         },
  59.         deleteContact: function () {
  60.            var removedType = this.model.get("type").toLowerCase();
  61.             this.model.destroy();
  62.             this.remove();
  63.             if (_.indexOf(directory.getTypes(), removedType) === -1) {
  64.                 directory.$el.find("#filter select").children("[value='" + removedType + "']").remove();
  65.             }
  66.         },
  67.         editContact: function () {
  68.             this.$el.html(this.editTemplate(this.model.toJSON()));
  69.             var newOpt = $("<option/>", {
  70.                 html: "<em>Add new...</em>",
  71.                 value: "addType"
  72.             });
  73.             this.select = directory.createSelect().addClass("type")
  74.                 .val(this.$el.find("#type").val()).append(newOpt)
  75.                 .insertAfter(this.$el.find(".name"));
  76.             this.$el.find("input[type='hidden']").remove();
  77.  
  78.            
  79.         },
  80.         saveEdits: function (e) {
  81.            e.preventDefault();
  82.             var formData = {},
  83.                 prev = this.model.previousAttributes();
  84.             $(e.target).closest("form").find(":input").not(':button').add(".photo").each(function () {
  85.                 var el = $(this);
  86.                 formData[el.attr("class")] = el.val();
  87.             });
  88.             if (formData.photo === "") {
  89.                 delete formData.photo;
  90.             }
  91.             this.model.set(formData);
  92.             this.render();
  93.             if (prev.photo === "/img/placeholder.png") {
  94.                 delete prev.photo;
  95.             }
  96.             _.each(contacts, function (contact) {
  97.                 if (_.isEqual(contact, prev)) {
  98.                     contacts.splice(_.indexOf(contacts, contact), 1, formData);
  99.                 }
  100.             });
  101.         },
  102.  
  103.         addType: function() {
  104.             if (this.select.val() === "addType") {
  105.                 this.select.remove();
  106.                 $("<input />", {
  107.                     "class": "type"
  108.                 }).insertAfter(this.$el.find(".name")).focus();
  109.             }
  110.         },
  111.         cancelEdit: function () {
  112.             this.render();
  113.         },*/
  114.     });
  115. /*
  116.     var ContactsRouter = Backbone.Router.extend({
  117.         routes: {
  118.             "filter/:type": "urlFilter"
  119.         },
  120.         urlFilter: function (type) {
  121.             directory.filterType = type;
  122.             directory.trigger("change:filterType");
  123.         }
  124.     });
  125. */
  126.     var DirectoryView = Backbone.View.extend({
  127.         el: $("#contacts"),
  128.         initialize: function () {
  129.  
  130.             this.$el.append('tst');
  131.             console.log('dir init');
  132.             $("#contacts").html('sas');
  133.             console.log($("#contacts"));
  134.            
  135.             this.collection = new Directory(contacts);
  136.             this.render();
  137.  
  138.             console.log(this.$el.find("#filter"));
  139.  
  140.             $('#filter').html('sasd');
  141.             this.$el.find("#filter").append('why this does not work?????');
  142.            
  143.             console.log('select');
  144.             console.log(this.createSelect());
  145.  
  146.             this.$el.find("#filter").append(this.createSelect());
  147.             this.on("change:filterType", this.filterByType, this);
  148.             this.collection.on("reset", this.render, this);
  149.             this.collection.on("add", this.renderContact, this);
  150.             this.collection.on("remove", this.removeContact, this);
  151.         },/*
  152.         render: function () {
  153.  
  154.             console.log('render');
  155.  
  156.             this.$el.find("article").remove();
  157.  
  158.             console.log(this.$el);
  159.  
  160.  
  161.             var that = this;
  162.             _.each(this.collection.models, function (item) {
  163.                 that.renderContact(item);
  164.             }, this);
  165.         },
  166.         renderContact: function (item) {
  167.  
  168.             console.log('renderContact');
  169.  
  170.             var contactView = new ContactView({
  171.                 model: item
  172.             });
  173.            // console.log(this.$el);
  174.            // console.log(contactView.render());
  175.             this.$el.append(contactView.render().el);
  176.  
  177.  
  178.         },*/
  179.         getTypes: function () {
  180.             return _.uniq(this.collection.pluck("type"), false, function (type) {
  181.                 return type.toLowerCase();
  182.             });
  183.         },
  184.         createSelect: function () {
  185.             var filter = this.$el.find("#filter"),  // where is fileter used there?
  186.                 select = $("<select/>", {
  187.                     html: "<option value='all'>All</option>"
  188.                 });
  189.                
  190.             console.log(select);
  191.             //console.log(this.getTypes());
  192.  
  193.             _.each(this.getTypes(), function (item) {
  194.  
  195.                 var option = $("<option/>", {
  196.                     value: item.toLowerCase(),
  197.                     text: item.toLowerCase()
  198.                 }).appendTo(select);
  199.             });
  200.  
  201.              console.log(select);
  202.  
  203.  
  204.             return select;
  205.         },/*
  206.         events: {
  207.             "change #filter select": "setFilter",
  208.             "click #add": "addContact",
  209.             "click #showForm": "showForm"
  210.         },
  211.         setFilter: function (e) {
  212.             this.filterType = e.currentTarget.value;
  213.             this.trigger("change:filterType");
  214.         },
  215.         filterByType: function () {
  216.             if (this.filterType === "all") {
  217.                 this.collection.reset(contacts);
  218.                 contactsRouter.navigate("filter/all");
  219.             } else {
  220.                 this.collection.reset(contacts, { silent: true });
  221.                 var filterType = this.filterType,
  222.                     filtered = _.filter(this.collection.models, function (item) {
  223.                         return item.get("type") === filterType;
  224.                 });
  225.                 this.collection.reset(filtered);
  226.                 contactsRouter.navigate("filter/" + filterType);
  227.             }
  228.         },
  229.         addContact: function (e) {
  230.             e.preventDefault();
  231.             var newModel = {};
  232.             $("#addContact").children("input").each(function (i, el) {
  233.                 if ($(el).val() !== "") {
  234.                     newModel[el.id] = $(el).val();
  235.               }
  236.             });
  237.             contacts.push(newModel);
  238.             if (_.indexOf(this.getTypes(), newModel.type) === -1) {
  239.                  this.collection.add(new Contact(newModel));
  240.                 this.$el.find("#filter").find("select").remove().end().append(this.createSelect());
  241.             } else {
  242.                 this.collection.add(new Contact(newModel));
  243.             }
  244.         },
  245.         removeContact: function (removedModel) {
  246.             var removed = removedModel.attributes;
  247.             if (removed.photo === "/img/placeholder.png") {
  248.                 delete removed.photo;
  249.             }
  250.             _.each(contacts, function (contact) {
  251.                 if (_.isEqual(contact, removed)) {
  252.                     contacts.splice(_.indexOf(contacts, contact), 1);
  253.                 }
  254.             });
  255.         },
  256.         showForm: function () {
  257.  
  258.             console.log('tetst');
  259.             this.$el.find("#addContact").slideToggle();
  260.         }
  261.         */
  262.     });
  263.  
  264.     var directory = new DirectoryView();
  265.    // var contactsRouter = new ContactsRouter();
  266.    // Backbone.history.start();
  267.  
  268. } (jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement