var observer = (function () {
var listeners = [],
subscribers = [];
return {
// Methods that manages event-listeners
subscribe : function (listener) {
"use strict";
var i;
for (i in listener) {
var check = $.inArray(i, subscribers);
console.log(check);
if (check === -1) {
subscribers.push(i);
listeners.push(listener[i]);
}
}
},
publish : function (args) {
"use strict";
for (var i = 0; i < listeners.length; i++) {
listeners[i]();
}
}
}
}());
var product_model = (function(observer){
// Private area
var addItem = $('#add-item'),
removeItem = $('#rm-item'),
products = [],
groups = [],
getProducts = function () {
return products;
},
/**
* Ajax call to get all availible products from db
*/
setProducts = function (k,v) {
for (var i in v) {
var product_group = v[i];
$.each(product_group.product_data, function(k,v) {
products.push(v);
});
}
},
setGroups = function (v) {
$.each(v, function(k,v){
groups.push(v);
});
// Fire event when group-array is populated
observer.publish('getGroup');
},
getGroups = function() {
return groups;
},
/**
* Ajax call to get all availible products from db
*/
get_all_products = function (url, showCart) {
var url = url;
$.ajax({
url : url,
type : "POST",
data : {get_products : "1"},
dataType : "json",
success : function(data) {
//console.log(data);
$.each(data, function(k,v){
setGroups(v);
setProducts(k,v);
});
if (showCart == 1) {
showCartPopup();
}
tableSorter();
chooseNumberOfProducts();
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_all_purchases = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_customer_summary : "1"},
dataType : "json",
success : function(data) {
template = $('#customerSummaryTpl').html(),
html = Mustache.to_html(template, data);
$('.purchase-history').html(html);
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_most_popular = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_most_popular : "1"},
dataType : "json",
success : function(data) {
template = $('#mostpopularTpl').html(),
html = Mustache.to_html(template, data);
$('.most-popular-wrapper').html(html);
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_customer_debt = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_customer_debt : "1"},
dataType : "json",
success : function(data) {
template = $('#customerDebtTpl').html(),
html = Mustache.to_html(template, data);
$('.total-purchase-overview').html(html)
}
});
},
/**
* Functionality for add/remove products
*/
chooseNumberOfProducts = function() {
addItem.live('click', function (){
var inputfield = $(this).siblings('input');
if (!inputfield.val()) {
inputfield.val(1)
} else {
inputfield.val(parseInt(inputfield.val()) + 1);
}
});
removeItem.live('click', function (){
var inputfield = $(this).siblings('input');
if (inputfield.val() > 1) {
inputfield.val(parseInt(inputfield.val()) - 1);
} else {
inputfield.val('');
}
});
},
showCartPopup = function () {
$("#checkout-btn").click( function() {
$("#dialog-receipt").dialog('open');
});
$("#dialog-receipt").dialog({
autoOpen: false,
resizable: true,
width: 500,
modal: true,
title: 'Receipt',
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
},
"Accept": function() {
$( this ).dialog( "close" );
}
}
});
},
tableSorter = function () {
$(".customer .tablesorter").tablesorter({
headers: {
3: {
// disable sorting on the fourth column
sorter: false
}
},
sortList : [[0,0]]
});
}
return {
get_all_products : get_all_products,
get_all_purchases : get_all_purchases,
get_most_popular : get_most_popular,
get_customer_debt : get_customer_debt,
setProducts : setProducts,
getProducts : getProducts,
setGroups : setGroups,
getGroups : getGroups
};
}(observer));
var product_view = (function(model, observer){
// Private area
var model = model,
observer = observer,
renderProductTpl = function () {
var data = { product_groups : model.getGroups()}
var template = $('#productsTpl').html(),
html = Mustache.to_html(template, data);
$('.products-list').html(html);
// listen to publish-event and execute this
// from observer-object when model publish event.
observer.subscribe({
'getGroups' : function(){renderProductTpl();}
});
}
return {
renderProductTpl : renderProductTpl,
observer : observer
};
}(product_model, observer));