Created
August 23, 2011 01:07
-
-
Save ctide/1164050 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
render: function(config){ | |
// default to empty | |
config = config || {}; | |
var filteredCollection, | |
contactsEl, contactTemplate, contactsHTML, | |
searchFilter, addContactToHTML; | |
var that = this; | |
filteredCollection = this.collection; | |
contactsEl = $("#contacts"); | |
countEl = $("#count"); | |
contactsEl.html(''); | |
contactsHTML = ""; | |
/** | |
* Truthy function for filtering down our collection based on config | |
* @param c {Object} Contact object | |
* @returns {Boolean} Pass or fail | |
*/ | |
searchFilter = function(c) { | |
// test to see if we have a query, otherwise everything passes | |
if (typeof(config.q) == "undefined") return true; | |
else config.q = (config.q+'').toLowerCase(); | |
// make everything lowercase so search isn't case sensititive | |
var name = c.get('name'); | |
if (name) name = name.toLowerCase(); | |
var email = c.get('email'); | |
if (email) email = email.toLowerCase(); | |
//search by name | |
if (typeof(name) != "undefined" && name.indexOf(config.q) != -1) return true; | |
// search by email | |
if(typeof(email) != "undefined" && email.indexOf(config.q) != -1) return true; | |
// search by twitter handle | |
if(typeof(twitterHandle) != "undefined" && twitterHandle.indexOf(config.q) != -1) return true; | |
// search by facebook handle | |
if(typeof(facebookHandle) != "undefined" && facebookHandle.indexOf(config.q) != -1) return true; | |
return false; | |
}; | |
// I could put this in a script tag on the page, | |
// but i kind of like being able to comment lines | |
contactTemplate = '<li class="contact" data-cid="<%= id %>">'; | |
contactTemplate += '<div class="contactSummary"><img src="<% if (typeof(smPhoto) != "undefined" ) { %><%= smPhoto %><% } else { %>/static/img/lock.png<% } %>"/>'; | |
contactTemplate += '<strong><% if (typeof(name) != "undefined") { %><%= name %><% } %></strong>'; | |
contactTemplate += '</div>'; | |
contactTemplate += '<div class="contactActions">'; | |
contactTemplate += '<% if (typeof(email) != "undefined") { %><a href="mailto:<%= email %>" target="_b" class="social_link email">Email</a><% } %> '; | |
contactTemplate += '<% if (typeof(facebookLink) != "undefined") { %><a href="<%= facebookLink %>" class="social_link facebook" target="_b">Facebook Profile</a><% } %>'; | |
contactTemplate += '<% if (typeof(twitterHandle) != "undefined" && typeof(twitterHandle.data.screen_name) != "undefined") { %><a href="http://twitter.com/<%= twitterHandle.data.screen_name %>" class="social_link twitter" target="_b">Twitter Profile</a><% } %>'; | |
contactTemplate += '<% if (typeof(github) != "undefined" && typeof(github.data.login) != "undefined") { %><a href="http://github.com/<%= github.data.login %>" class="social_link github" target="_b">GitHub Profile</a><% } %>'; | |
contactTemplate += '</div>'; | |
contactTemplate += '<br/><pre><%= json %></pre>'; | |
contactTemplate += '<div class="clear"></div></li>'; | |
addContactToHTML = function(c) { | |
// create a simple json obj to use for creating the template (if necessary) | |
if (typeof(c.get('html')) == "undefined") { | |
var tmpJSON = c.toJSON(); | |
if (typeof(tmpJSON.name) == "undefined") { | |
tmpJSON.name = tmpJSON.email; | |
} | |
tmpJSON.smPhoto = that.getPhoto(tmpJSON, false); | |
tmpJSON.photo = that.getPhoto(tmpJSON, true); | |
tmpJSON.json = JSON.stringify(tmpJSON, null, 2); | |
// cache compiled template to the model | |
var compiledTemplate = _.template(contactTemplate, tmpJSON); | |
c.set({'html': compiledTemplate}); | |
contactsEl.append(compiledTemplate); | |
// contactsHTML += compiledTemplate; | |
} else { | |
// just get the rendered html from our model | |
contactsEl.append(c.get('html')); | |
// contactsHTML += c.get('html'); | |
} | |
}; | |
var tmp = filteredCollection.filter(searchFilter); | |
var sortFn = function(c) { | |
if (c.get(that.sortType)) { | |
return c.get(that.sortType); | |
} | |
return "zzz"; // force to the end of the sort | |
}; | |
tmp = _.sortBy(tmp, sortFn); | |
_.each(tmp, addContactToHTML); | |
// contactsEl.html(contactsHTML); | |
countEl.html(tmp.length); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment