Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
- <script src="https://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.js"></script>
- <script src="https://github.com/SteveSanderson/knockout/raw/master/build/output/knockout-latest.debug.js"></script>
- <script>
- // Contacts Knockout Model
- var contactsModel = {
- //contacts: ko.observableArray([]),
- contactsLoaded: ko.observable(false),
- contactLoaded: ko.observable(false),
- photo_src: ko.observable('http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png'),
- fname: ko.observable(),
- lname: ko.observable(),
- email: ko.observable(),
- full_name: function(fname, lname, email) {
- // For email contacts w/o names, say no name and not null
- var full_name = email;
- if (fname !== null && lname !== null) {
- full_name = fname + ' <b>' + lname + '</b>';
- }
- return full_name;
- },
- contactsByLetter: ko.observableArray(),
- letterIndex: {}
- };
- //add a contact in the right spot under the right letter
- contactsModel.addContact = function(newContact) {
- //grab first character
- var firstLetter = (newContact.lname || "").charAt(0).toUpperCase();
- //if it is a number use #
- if (!isNaN(firstLetter)) {
- firstLetter = "#";
- }
- //do we already have entries for this letter
- if (!this.letterIndex[firstLetter]) {
- //new object to track this letter's contacts
- var letterContacts = {
- letter: firstLetter,
- contacts: ko.observableArray([])
- };
- this.letterIndex[firstLetter] = letterContacts; //easy access to it
- //put the numbers at the end
- if (firstLetter === "#") {
- this.contactsByLetter.push(letterContacts);
- } else {
- //add the letter in the right spot
- for (var i = 0, lettersLength = this.contactsByLetter().length; i < lettersLength; i++) {
- var letter = this.contactsByLetter()[i].letter;
- if (letter === "#" || firstLetter < letter) {
- break;
- }
- }
- this.contactsByLetter.splice(i, 0, letterContacts);
- }
- }
- var contacts = this.letterIndex[firstLetter].contacts;
- //now we have a letter to add our contact to, but need to add it in the right spot
- var newContactName = newContact.lname + " " + newContact.fname;
- for (var j = 0, contactsLength = contacts().length; j < contactsLength; j++) {
- var contactName = contacts()[j].lName + " " + contacts()[j].fName;
- if (newContactName < contactName) {
- break;
- }
- }
- //add the contact at the right index
- contacts.splice(j, 0, newContact);
- }.bind(contactsModel);
- var current_page = 1;
- function ajaxContacts(callback) {
- /*
- $.ajax({
- url: '/echo/json/',
- data: "page=" + current_page,
- beforeSend: function() {},
- success: function(dataJS) {
- console.log(current_page);
- dataJS = dummydata(current_page);
- ko.utils.arrayForEach(dataJS.contactList, function(c) {
- contactsModel.addContact(c);
- //contactsModel.contacts.push(c);
- });
- current_page = current_page + 1;
- if (dataJS.page < dataJS.total_pages) {
- ajaxContacts(callback);
- }
- }
- });
- */
- ko.utils.arrayForEach(dummydata(1).contactList, function(c) {
- contactsModel.addContact(c);
- //contactsModel.contacts.push(c);
- });
- }
- // Dummy Data
- function dummydata(page) {
- return {
- "total_pages": 10,
- "page": page,
- "contactList": [{
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Bond",
- "id": 241,
- "fname": "James",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Yor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Bond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Yor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Aond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Tor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "yond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Nor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Dond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "AAAor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "LLond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "dor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "zond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Yor",
- "id": 241,
- "fname": "Sam",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Bond",
- "id": 241,
- "fname": "dames",
- "email": "xxx@yahoo.com"},
- {
- "photo": "http://homepage.mac.com/millhouse/Family%20Tree/images/PersonListIcon.png",
- "lname": "Car",
- "id": 241,
- "fname": "aames",
- "email": "xxx@yahoo.com"}]
- };
- }
- $(function(){
- ko.applyBindings(contactsModel, document.getElementById("view-panel-contacts"));
- });
- ajaxContacts();
- </script>
- </head>
- <body>
- <div id="view-panel-contacts">
- <div id="contact-list" data-bind="template: { name: 'letterTmpl', foreach: contactsModel.contactsByLetter() }"></div>
- </div>
- <script id="letterTmpl" type="text/html">
- <div class="contact-header">
- <span data-bind="text: letter"></span>
- </div>
- <div data-bind="template: { name: 'contactTmpl', foreach: contacts }"></div>
- </script>
- <script id="contactTmpl" type="text/html">
- <div name="${id}" id="contact_${id}" class="contact-row">
- <a href="#/contacts/${id}">
- <div class="photo">
- <img src="${photo}" />
- </div>
- <div class="name">{{html contactsModel.full_name(fname, lname, email)}}</div>
- </a>
- </div>
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement