Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4346503 to your computer and use it in GitHub Desktop.
Save anonymous/4346503 to your computer and use it in GitHub Desktop.
Vehicle ads viewed recently
$(document).ready(function(){
window.RecentlyViewed = (function(){
recent_ads = (function(){
var list = [];
$.each( $("#recently_viewed li"), function(i, li){
list.push( new RecentAd($(li).attr('id')) );
});
return list;
}());
var container = $("#lead_form_container");
var form = $(container).find('form');
function RecentAd(vin){
this.vin = vin;
this.el = $("#" + vin);
this.hasMessagedDealer = function(){
$(this.el).find(".message_dealer.btn").css({'color':'green'}).after('<i style="color:green; font-weight:bold" class="icon-search">message sent</i>').remove();
};
};
var find = function(vin){
foundAd = null;
$.each(this.recent_ads, function(i,ad){
if (ad.vin === vin){
foundAd = ad;
}
});
return foundAd;
}
var fetchLeadForm = function(url){
// fetch the lead form from the given ad page
$.get(url,function(dom){
// parse out the form and change its action to the /leads.json POST path
form = $(dom).find("form:first").attr('action', '/leads.json');
container.html(form);
})
}
function setError(errorMessage) {
var formError = document.getElementById("formError");
setTextContent(formError, errorMessage);
var regSubmit = document.getElementById("regSubmit");
$(form).find('input[type="submit"]').disabled = true;
}
consoleLog = function(obj){
console.log(obj);
}
var messageDealer = function(){
params = form.serialize();
message = $.post("/leads.json", params);
message.complete(function(response){
vin = JSON.parse(response.responseText).vin;
RecentlyViewed.find(vin).hasMessagedDealer();
form.remove();
})
}
return {
find: find,
fetchLeadForm: fetchLeadForm,
messageDealer: messageDealer,
recent_ads: recent_ads
}
}());
});
$("#recently_viewed .message_dealer.btn").click(function(event){
ad_page_url = $(event.target).parent().find('.recent_thumbnail a').attr('href');
RecentlyViewed.fetchLeadForm(ad_page_url);
});
$("#lead_form_container form").live('submit',function(e){
e.preventDefault();
RecentlyViewed.messageDealer();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment