Skip to content

Instantly share code, notes, and snippets.

@nhp
Created December 6, 2012 13:14
Show Gist options
  • Save nhp/4224372 to your computer and use it in GitHub Desktop.
Save nhp/4224372 to your computer and use it in GitHub Desktop.
/**
* Send ajax request to the Magento store in order to insert dynamic content into the
* static page delivered from Varnish
*
* @author Fabrizio Branca
*/
$.noConflict();
jQuery(document).ready(function($) {
var data = {
getBlocks: {}
};
// add placeholders
var counter = 0;
$('.placeholder').each(function() {
var id = $(this).attr('id');
if (!id) {
// create dynamic id
id = 'ph_' + counter;
$(this).attr('id', id);
}
var rel = $(this).attr('rel');
if (rel) {
data.getBlocks[id] = rel;
counter++;
} else {
throw 'Found placeholder without rel attribute';
}
});
// add current product
if (typeof CURRENTPRODUCTID !== 'undefined' && CURRENTPRODUCTID) {
data.currentProductId = CURRENTPRODUCTID;
}
// E.T. phone home
if (typeof data.currentProductId !== 'undefined' || counter > 0) {
$.ajax({
url: AJAXHOME_URL,
data: data,
complete: function (response) {
for(var id in $.parseJSON(response.responseText).blocks) {
$('#' + id).html($.parseJSON(response.responseText).blocks[id]);
}
// inject session if (TODO: check if this is really needed)
// $.cookie('frontend', response.sid, { path: '/' });
// TODO: trigger event
},
datatype: 'json',
cache: false,
type: 'GET'
});
/*
$.get(
AJAXHOME_URL,
data,
function (response) {
for(var id in response.blocks) {
$('#' + id).html(response.blocks[id]);
}
// inject session if (TODO: check if this is really needed)
// $.cookie('frontend', response.sid, { path: '/' });
// TODO: trigger event
},
'json'
);
*/
....
}
**
* Send ajax request to the Magento store in order to insert dynamic content into the
* static page delivered from Varnish
*
* @author Fabrizio Branca
*/
var jqd = jQuery.noConflict();
jqd(document).ready(function($) {
var data = { getBlocks: {} };
// add placeholders
jqd('.placeholder').each(function() {
data.getBlocks[$(this).attr('id')] = $(this).attr('rel');
});
// add current product
if (typeof CURRENTPRODUCTID !== 'undefined' && CURRENTPRODUCTID) {
data.currentProductId = CURRENTPRODUCTID;
}
// E.T. phone home
jqd.get(
AJAXHOME_URL,
data,
function (data) {
for(var id in data.blocks) {
$('#' + id).html(data.blocks[id]);
}
$.cookie('frontend', data.sid, { path: '/' });
},
'json'
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment