var goods = model.data.zakazi.filter(function (goods) {
return goods.ID === ID;
})[0];
Такая конструкция
var controller = {
onAtbClick: function(e, model) {
var ID = $(this).data("main-id");
console.log(data );
var goods = model.data.zakazi.filter(function (goods) {
return goods.ID === ID;
})[0];
model.data.zakazi.push({
ID: ID,
quantity: 1
});
},
saveData: function(data) {
localStorage.setItem( 'g_zakazi', JSON.stringify( data ) );
}
}
После успешного ajax, запускается функция
function toTopAfterAjaxUpdatePagination()
{
var scrollGoods = $('section.goods').offset().top;
$(document).scrollTop(scrollGoods - 50);
$('.goods__list').children('.goods__item').find('.goods__tocart').bind('click', controller.onAtbClick);
console.log('Ураааа поднялись');
var userData = JSON.parse(localStorage.getItem('g_zakazi')) || {
title: 'Welcome',
juzer_id: $('#juzer-id').text(),
subtotal: 0,
zakazi: []
};
rivets.bind($('#rivetsSpace'), {data: userData, controller: controller});
console.log(userData );
}
var data = JSON.parse(localStorage.getItem('g_zakazi')) || {
title: 'Welcome',
juzer_id: $('#juzer-id').text(),
subtotal: 0,
zakazi: []
};
rivets.bind(
document.querySelector('#rivetsSpace'),
{
data: data,
controller: controller
}
);
<p data-text="Добавить в корзину" class="goods__tocart" data-main-id="179" rv-on-click="controller.onAtbClick"><span>Добавить</span></p>
$.ajax({
url: '/demo.php',
type: 'POST',
data: data,
success: function (html) {
$goods_list.empty().append(html);
rivets.bind($goods_list, model);
toTopAfterAjaxUpdatePagination();
}
});
var $ = jQuery;
$(function(){
var $goods_list = $('ul.goods__list'),
model = {
data: JSON.parse(localStorage.getItem('g_zakazi')) || {
title: 'Welcome',
//juzer_id: $('#juzer-id').text(),
subtotal: 0,
zakazi: []
},
controller: controller
};
rivets.bind(document.querySelector('#rivetsSpace'), model);
$('.goods__pagination').on('click', '.goods__page', function(){
console.log('Клик по пагинации');
var data = { id: $(this).text() };
$.ajax({
url: '/demo.php',
type: 'POST',
data: data,
success: function (html) {
//console.log(data);
$goods_list.empty().append(html);
rivets.bind($goods_list, model);
toTopAfterAjaxUpdatePagination();
}
});
});
});
function returnFalse(){
console.log('Выходим');
return false;
}
var controller = {
onAtbClick: function(e, model) {
var ID = $(this).data("id");
var goods = model.data.zakazi.filter(function (goods) {
return goods.ID === ID;
})[0];
if (goods) {
goods.quantity++;
returnFalse();
} else {
model.data.zakazi.push({
ID: ID,
quantity: 1
});
}
model.controller.updatePrice(model.data);
model.controller.saveData(model.data);
},
updatePrice: function(data) {
var zakazi = data.zakazi,
product,
subtotal = 0;
data.subtotal = subtotal;
},
removeGoods: function(e, model) {
e.stopImmediatePropagation();
var index = model.index,
zakazi = model.data.zakazi;
zakazi.splice(index, 1);
model.controller.saveData(model.data);
},
saveData: function(data) {
localStorage.setItem( 'g_zakazi', JSON.stringify( data ) );
}
}
function toTopAfterAjaxUpdatePagination() {
var scrollGoods = $('section.goods').offset().top;
$(document).scrollTop(scrollGoods - 50);
}
rivets.formatters.length = function(val) {
return val.length;
};
window.items = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
...
]
model.$goods_list.append( controller.processGoods(window.items, model) );
...
var controller = {
processGoods: function(data, model) {
var elements = new Array(data.length),
temp_fragment = document.createDocumentFragment();
for (var i = 0; i < data.length; i++) {
elements[ i ] = model.controller.createElementFromTemplate('goods__item', model);
rivets.bind(elements[i], Object.assign({ item: data[i] }, model));
temp_fragment.appendChild( elements[ i ] );
}
return temp_fragment;
},
createElementFromTemplate: function(item, model) {
return document.importNode(model.templates[ item ].content.children[0], true);
},
...
}
$('.goods__pagination').on('click', '.goods__page', function() {
var data = {
id: $(this).text()
};
$.ajax({
url: '/demo.php',
type: 'POST',
data: data,
dataType: 'json',
success: function(data) {
model.$goods_list.empty().append( controller.processGoods(data, model) );
toTopAfterAjaxUpdatePagination();
}
});
});