2011-02-04 03:15:16 +00:00
|
|
|
(function() {
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
var Product = Backbone.Model.extend({
|
|
|
|
toJSON: function() {
|
|
|
|
return _.extend(_.clone(this.attributes), {cid: this.cid})
|
2010-12-09 00:21:22 +00:00
|
|
|
}
|
2011-02-04 03:15:16 +00:00
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
var ProductCollection = Backbone.Collection.extend({
|
|
|
|
model: Product,
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
comparator: function(item) {
|
|
|
|
return item.get('title');
|
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
var CartItem = Backbone.Model.extend({
|
|
|
|
update: function(amount) {
|
|
|
|
this.set({'quantity': this.get('quantity') + amount});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var Cart = Backbone.Collection.extend({
|
|
|
|
model: CartItem,
|
|
|
|
getByProductId: function(pid) {
|
|
|
|
return this.detect(function(obj) { return (obj.get('product').cid == pid); });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var CartView = Backbone.View.extend({
|
|
|
|
el: $('.cart-info'),
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.collection.bind('change', _.bind(this.render, this));
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var sum = this.collection.reduce(function(m, n) { return m + n.get('quantity'); }, 0);
|
|
|
|
this.el
|
|
|
|
.find('.cart-items').text(sum).end()
|
|
|
|
.animate({paddingTop: '30px'})
|
|
|
|
.animate({paddingTop: '10px'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var ProductListView = Backbone.View.extend({
|
|
|
|
el: $('#main'),
|
|
|
|
indexTemplate: $("#indexTmpl").template(),
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var self = this;
|
|
|
|
this.el.fadeOut('fast', function() {
|
|
|
|
self.el.html($.tmpl(self.indexTemplate, self.model.toJSON()));
|
|
|
|
self.el.fadeIn('fast');
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
var ProductView = Backbone.View.extend({
|
|
|
|
el: $('#main'),
|
|
|
|
itemTemplate: $("#itemTmpl").template(),
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
initialize: function(options) {
|
|
|
|
this.cart = options.cart;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var cart_item = this.cart.getByProductId(this.model.cid);
|
|
|
|
if (_.isUndefined(cart_item)) {
|
|
|
|
cart_item = new CartItem({product: this.model, quantity: 0});
|
|
|
|
this.cart.add(cart_item, {silent: true});
|
|
|
|
}
|
|
|
|
cart_item.update(parseInt($('.uqf').val()));
|
|
|
|
},
|
|
|
|
|
|
|
|
updateOnEnter: function(e) {
|
|
|
|
if (e.keyCode == 13) {
|
|
|
|
return this.update(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
|
|
|
"keypress .uqf" : "updateOnEnter",
|
|
|
|
"click .uq" : "update",
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var self = this;
|
|
|
|
this.el.fadeOut('fast', function() {
|
|
|
|
self.el.html($.tmpl(self.itemTemplate, self.model.toJSON()));
|
|
|
|
self.el.fadeIn('fast');
|
2010-12-09 00:21:22 +00:00
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2011-02-04 03:15:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var BackboneStore = Backbone.Controller.extend({
|
|
|
|
_index: null,
|
|
|
|
_products: null,
|
|
|
|
_cart :null,
|
|
|
|
routes: {
|
|
|
|
"": "index",
|
|
|
|
"item/:id": "item",
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function(data) {
|
|
|
|
this._cart = new Cart();
|
|
|
|
new CartView({collection: this._cart});
|
|
|
|
this._products = new ProductCollection(data);
|
|
|
|
this._index = new ProductListView({model: this._products});
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
index: function() {
|
|
|
|
this._index.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
item: function(id) {
|
|
|
|
var product = this._products.getByCid(id);
|
|
|
|
if (_.isUndefined(product._view)) {
|
|
|
|
product._view = new ProductView({model: product,
|
|
|
|
cart: this._cart});
|
|
|
|
}
|
|
|
|
product._view.render();
|
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
$(document).ready(function() {
|
|
|
|
var fetch_items = function() {
|
|
|
|
return $.ajax({
|
|
|
|
url: 'data/items.json',
|
|
|
|
data: {},
|
|
|
|
contentType: "application/json; charset=utf-8",
|
|
|
|
dataType: "json"
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$.when(fetch_items()).then(function(data) {
|
|
|
|
new BackboneStore(data);
|
|
|
|
Backbone.history.start();
|
|
|
|
});
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
}).call(this);
|