Skip to content

Instantly share code, notes, and snippets.

@vchub
Last active December 21, 2015 19:39
Show Gist options
  • Save vchub/6356169 to your computer and use it in GitHub Desktop.
Save vchub/6356169 to your computer and use it in GitHub Desktop.
recommendation request and rendering
// Item page recommendation request and rendering
_gravity.push({type : 'recommendation',
callback: callback_fn,
numberLimit: 6,
scenarioId: "ITEM_PAGE",
currentItemId: "1111", // change to item_id in feed
itemOnPage: [], // [] of itemIds already displayed on the page
resultNames: ["itemId"] // item attributes present in Item Catalog (feed)
});
function callback_fn(result) {
var div = document.getElementById("reco_box");
for (var i = 0; i < result.items.length; ++i) {
var item = result.items[i];
// create_item_element_of_reco_box - user defined function
var itemDiv = create_item_element_of_reco_box(item);
// add REC_CLICK event handling
itemDiv.onmousedown = function() {
_gravity = _gravity || [];
_gravity.push({
type: 'event',
eventType: 'REC_CLICK',
// for own reco-boxes that don't use gravity, a fixed value is used
// e.g. recommendationId: "OWN_ITEM_PAGE",
recommendationId: result.recommendationId,
itemId: item.itemid,
});
}
div.appendChild(itemDiv);
}
}
// Main page recommendation request and rendering
_gravity.push({type : 'recommendation',
callback: callback_fn,
numberLimit: 100,
scenarioId: "MAIN_PAGE",
// filter items you don't need to be recommended
// e.g. items on fixed position
itemOnPage: [111, 222, 333], // [] of itemIds already displayed on the page
resultNames: ["itemId"] // item attributes present in Item Catalog (feed)
});
function callback_fn(result) {
for (var i = 0; i < result.items.length; ++i) {
var item = result.items[i];
// choose a div for an item
var itemDiv = $("#item_div_" + i);
// render_item_div - user defined function that may request item data
// from a server and render the item
var itemDiv = render_item_div(item, itemDiv);
// add REC_CLICK event handling
itemDiv.onmousedown = function() {
_gravity = _gravity || [];
_gravity.push({
type: 'event',
eventType: 'REC_CLICK',
// for own reco-boxes that don't use gravity, a fixed value is used
// e.g. recommendationId: "OWN_MAIN_PAGE",
recommendationId: result.recommendationId,
itemId: item.itemid,
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment