Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. (function ($) {
  4.  
  5.   var contacts = [
  6.     {name: 'Contact 1', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'family'},
  7.     {name: 'Contact 2', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'family'},
  8.     {name: 'Contact 3', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'friend'},
  9.     {name: 'Contact 4', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'colleague'},
  10.     {name: 'Contact 5', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'family'},
  11.     {name: 'Contact 6', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'colleague'},
  12.     {name: 'Contact 7', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'friend'},
  13.     {name: 'Contact 8', address: '1, a street, a town, a city, AB12 3CD', tel: '0123456789', email: 'anemail@me.com', type: 'family'}
  14.   ];
  15.  
  16.   var Contact = Backbone.Model.extend({
  17.     defaults: {
  18.       photo: 'https://pp.userapi.com/c837126/v837126058/3cc4c/WqVM8moEpiQ.jpg'
  19.     }
  20.   });
  21.  
  22.   var ContactView = Backbone.View.extend({
  23.     tagName: 'div',
  24.     className: 'contact',
  25.     template: $('#contactTemplate').html(),
  26.  
  27.     render: function () {
  28.       var tmpl = _.template(this.template);
  29.       this.$el.html(tmpl(this.model.toJSON()));
  30.       return this;
  31.     }
  32.   });
  33.  
  34.   var Directory = Backbone.Collection.extend({
  35.     model: Contact
  36.   });
  37.  
  38.   var DirectoryView = Backbone.View.extend({
  39.     el: $('#contacts'),
  40.  
  41.     initialize: function () {
  42.       this.collection = new Directory(contacts);
  43.       // Для добавления элемента <select> на страницу, можем написать код в нашем главный шаблоне, в методе initialize
  44.       // this.$el.find("#filter").append(this.createSelect());
  45.       this.render();
  46.     },
  47.  
  48.     render: function () {
  49.       var that = this;
  50.       _.each(this.collection.models, function (item) {
  51.         that.renderContact(item);
  52.       }, this);
  53.     },
  54.  
  55.     renderContact: function (item) {
  56.       var contactView = new ContactView({
  57.         model: item
  58.       });
  59.       this.$el.append(contactView.render().el);
  60.     },
  61.  
  62.     // будет извлекать уникальные типы. вернет массив
  63.     // pluck извлекает все уникальные поля type из массива моделей
  64.     getTypes: function () {
  65.       return _.uniq(this.collection.pluck("type"), false, function (type) {
  66.         return type.toLowerCase();
  67.       });
  68.     },
  69.  
  70.     // создаст выпадающий список
  71.     createSelect: function () {
  72.       var filter = this.el.find("#filter"),
  73.         select = $("<select/>", {
  74.           html: "<option>All</option>"
  75.         });
  76.  
  77.       _.each(this.getTypes(), function (item) {
  78.         var option = $("<option/>", {
  79.           value: item.toLowerCase(),
  80.           text: item.toLowerCase()
  81.         }).appendTo(select);
  82.       });
  83.       return select;
  84.     }
  85.  
  86.  
  87.   });
  88.  
  89.   var directory = new DirectoryView();
  90.  
  91.  
  92. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement