2011-02-04 03:15:16 +00:00
|
|
|
(function() {
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var Product = Backbone.Model.extend({})
|
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-08-07 19:33:07 +00:00
|
|
|
initialize: function(models, options) {
|
|
|
|
this.url = options.url;
|
|
|
|
},
|
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
comparator: function(item) {
|
|
|
|
return item.get('title');
|
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var Item = Backbone.Model.extend({
|
2011-02-04 03:15:16 +00:00
|
|
|
update: function(amount) {
|
|
|
|
this.set({'quantity': this.get('quantity') + amount});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var ItemCollection = Backbone.Collection.extend({
|
|
|
|
model: Item,
|
|
|
|
getOrCreateItemForProduct: function(product) {
|
|
|
|
var i,
|
|
|
|
pid = product.get('id'),
|
|
|
|
o = this.detect(function(obj) {
|
|
|
|
return (obj.get('product').get('id') == pid);
|
|
|
|
});
|
|
|
|
if (o) {
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
i = new Item({'product': product, 'quantity': 0})
|
|
|
|
this.add(i, {silent: true})
|
|
|
|
return i;
|
2011-02-04 03:15:16 +00:00
|
|
|
},
|
2011-08-07 19:33:07 +00:00
|
|
|
getTotalCount: function() {
|
|
|
|
return this.reduce(function(memo, obj) {
|
|
|
|
return obj.get('quantity') + memo; }, 0);
|
|
|
|
},
|
|
|
|
getTotalCost: function() {
|
|
|
|
return this.reduce(function(memo, obj) {
|
|
|
|
return (obj.get('product').get('price') *
|
|
|
|
obj.get('quantity')) + memo; }, 0);
|
|
|
|
|
|
|
|
}
|
2011-02-04 03:15:16 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var _BaseView = Backbone.View.extend({
|
|
|
|
parent: $('#main'),
|
|
|
|
className: 'viewport',
|
2011-02-04 03:15:16 +00:00
|
|
|
|
|
|
|
initialize: function() {
|
2011-08-07 19:33:07 +00:00
|
|
|
this.el = $(this.el);
|
|
|
|
this.el.hide();
|
|
|
|
this.parent.append(this.el);
|
|
|
|
return this;
|
2011-02-04 03:15:16 +00:00
|
|
|
},
|
2011-08-07 19:33:07 +00:00
|
|
|
|
|
|
|
hide: function() {
|
|
|
|
if (this.el.is(":visible") === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
promise = $.Deferred(_.bind(function(dfd) {
|
|
|
|
this.el.fadeOut('fast', dfd.resolve)}, this)).promise();
|
|
|
|
this.trigger('hide', this);
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function() {
|
|
|
|
if (this.el.is(':visible')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise = $.Deferred(_.bind(function(dfd) {
|
|
|
|
this.el.fadeIn('fast', dfd.resolve) }, this)).promise();
|
|
|
|
|
|
|
|
this.trigger('show', this);
|
|
|
|
return promise;
|
2011-02-04 03:15:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var ProductListView = _BaseView.extend({
|
|
|
|
id: 'productlistview',
|
|
|
|
template: $("#store_index_template").html(),
|
|
|
|
|
|
|
|
initialize: function(options) {
|
|
|
|
this.constructor.__super__.initialize.apply(this, [options])
|
|
|
|
this.collection.bind('reset', _.bind(this.render, this));
|
|
|
|
},
|
2011-02-04 03:15:16 +00:00
|
|
|
|
|
|
|
render: function() {
|
2011-08-07 19:33:07 +00:00
|
|
|
this.el.html(_.template(this.template,
|
|
|
|
{'products': this.collection.toJSON()}))
|
2011-02-04 03:15:16 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var ProductView = _BaseView.extend({
|
|
|
|
id: 'productitemview',
|
|
|
|
template: $("#store_item_template").html(),
|
2011-02-04 03:15:16 +00:00
|
|
|
initialize: function(options) {
|
2011-08-07 19:33:07 +00:00
|
|
|
this.constructor.__super__.initialize.apply(this, [options])
|
|
|
|
this.itemcollection = options.itemcollection;
|
|
|
|
this.item = this.itemcollection.getOrCreateItemForProduct(this.model);
|
2011-02-04 03:15:16 +00:00
|
|
|
return this;
|
|
|
|
},
|
2011-08-07 19:33:07 +00:00
|
|
|
|
|
|
|
events: {
|
|
|
|
"keypress .uqf" : "updateOnEnter",
|
|
|
|
"click .uq" : "update",
|
|
|
|
},
|
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
update: function(e) {
|
|
|
|
e.preventDefault();
|
2011-08-07 19:33:07 +00:00
|
|
|
this.item.update(parseInt($('.uqf').val()));
|
2011-02-04 03:15:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateOnEnter: function(e) {
|
|
|
|
if (e.keyCode == 13) {
|
|
|
|
return this.update(e);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2011-08-07 19:33:07 +00:00
|
|
|
this.el.html(_.template(this.template, this.model.toJSON()));
|
2010-12-09 00:21:22 +00:00
|
|
|
return this;
|
|
|
|
}
|
2011-02-04 03:15:16 +00:00
|
|
|
});
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
var CartWidget = Backbone.View.extend({
|
|
|
|
el: $('.cart-info'),
|
|
|
|
template: $('#store_cart_template').html(),
|
|
|
|
|
|
|
|
initialize: function() {
|
|
|
|
this.collection.bind('change', _.bind(this.render, this));
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
console.log(arguments);
|
|
|
|
this.el.html(
|
|
|
|
_.template(this.template, {
|
|
|
|
'count': this.collection.getTotalCount(),
|
|
|
|
'cost': this.collection.getTotalCost()
|
|
|
|
})).animate({paddingTop: '30px'})
|
|
|
|
.animate({paddingTop: '10px'});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var BackboneStore = Backbone.Router.extend({
|
|
|
|
views: {},
|
|
|
|
products: null,
|
|
|
|
cart: null,
|
2011-02-04 03:15:16 +00:00
|
|
|
|
|
|
|
routes: {
|
|
|
|
"": "index",
|
2011-08-07 19:33:07 +00:00
|
|
|
"item/:id": "product",
|
2011-02-04 03:15:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function(data) {
|
2011-08-07 19:33:07 +00:00
|
|
|
this.cart = new ItemCollection();
|
|
|
|
new CartWidget({collection: this.cart});
|
|
|
|
|
|
|
|
this.products = new ProductCollection([], {
|
|
|
|
url: 'data/items.json'});
|
|
|
|
this.views = {
|
|
|
|
'_index': new ProductListView({
|
|
|
|
collection: this.products
|
|
|
|
})
|
|
|
|
};
|
|
|
|
$.when(this.products.fetch({reset: true}))
|
|
|
|
.then(function() { window.location.hash = ''; });
|
2011-02-04 03:15:16 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
hideAllViews: function () {
|
|
|
|
return _.select(
|
|
|
|
_.map(this.views, function(v) { return v.hide(); }),
|
|
|
|
function (t) { return t != null });
|
|
|
|
},
|
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
index: function() {
|
2011-08-07 19:33:07 +00:00
|
|
|
var view = this.views['_index'];
|
|
|
|
$.when(this.hideAllViews()).then(
|
|
|
|
function() { return view.show(); });
|
2011-02-04 03:15:16 +00:00
|
|
|
},
|
|
|
|
|
2011-08-07 19:33:07 +00:00
|
|
|
product: function(id) {
|
|
|
|
var product, v, view;
|
|
|
|
product = this.products.detect(function(p) { return p.get('id') == (id); })
|
|
|
|
view = ((v = this.views)['item.' + id]) || (v['item.' + id] = (
|
|
|
|
new ProductView({model: product,
|
|
|
|
itemcollection: this.cart}).render()));
|
|
|
|
$.when(this.hideAllViews()).then(
|
|
|
|
function() { view.show(); });
|
2011-02-04 03:15:16 +00:00
|
|
|
}
|
|
|
|
});
|
2010-12-09 00:21:22 +00:00
|
|
|
|
2011-02-04 03:15:16 +00:00
|
|
|
$(document).ready(function() {
|
2011-08-07 19:33:07 +00:00
|
|
|
new BackboneStore();
|
|
|
|
Backbone.history.start();
|
2011-02-04 03:15:16 +00:00
|
|
|
});
|
|
|
|
}).call(this);
|