The-Backbone-Store/thestore/store.js

212 lines
6.1 KiB
JavaScript
Raw Permalink Normal View History

(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
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;
},
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({
update: function(amount) {
2011-08-08 00:44:17 +00:00
this.set({'quantity': amount}, {silent: true});
this.collection.trigger('change', this);
},
price: function() {
console.log(this.get('product').get('title'), this.get('quantity'));
return this.get('product').get('price') * this.get('quantity');
}
});
2011-08-07 19:33:07 +00:00
var ItemCollection = Backbone.Collection.extend({
model: Item,
2011-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
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-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
getTotalCount: function() {
return this.reduce(function(memo, obj) {
return obj.get('quantity') + memo; }, 0);
},
2011-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
getTotalCost: function() {
return this.reduce(function(memo, obj) {
2011-08-08 00:44:17 +00:00
return obj.price() + memo; }, 0);
2011-08-07 19:33:07 +00:00
}
});
2011-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
var _BaseView = Backbone.View.extend({
parent: $('#main'),
className: 'viewport',
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-08-07 19:33:07 +00:00
hide: function() {
if (this.el.is(":visible") === false) {
return null;
}
promise = $.Deferred(_.bind(function(dfd) {
2011-08-08 00:44:17 +00:00
this.el.fadeOut('fast', dfd.resolve)}, this));
return promise.promise();
2011-08-07 19:33:07 +00:00
},
show: function() {
if (this.el.is(':visible')) {
return;
}
promise = $.Deferred(_.bind(function(dfd) {
2011-08-08 00:44:17 +00:00
this.el.fadeIn('fast', dfd.resolve) }, this))
return promise.promise();
}
});
2011-08-08 00:44:17 +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));
},
render: function() {
2011-08-07 19:33:07 +00:00
this.el.html(_.template(this.template,
{'products': this.collection.toJSON()}))
return this;
}
});
2010-12-09 00:21:22 +00:00
2011-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
var ProductView = _BaseView.extend({
id: 'productitemview',
template: $("#store_item_template").html(),
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);
return this;
},
2011-08-07 19:33:07 +00:00
events: {
"keypress .uqf" : "updateOnEnter",
"click .uq" : "update",
},
update: function(e) {
e.preventDefault();
2011-08-08 00:44:17 +00:00
this.item.update(parseInt(this.$('.uqf').val()));
},
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-08-08 00:44:17 +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() {
this.el.html(
_.template(this.template, {
'count': this.collection.getTotalCount(),
'cost': this.collection.getTotalCost()
})).animate({paddingTop: '30px'})
.animate({paddingTop: '10px'});
}
});
2011-08-08 00:44:17 +00:00
2011-08-07 19:33:07 +00:00
var BackboneStore = Backbone.Router.extend({
views: {},
products: null,
cart: null,
routes: {
"": "index",
2011-08-07 19:33:07 +00:00
"item/:id": "product",
},
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 = ''; });
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 });
},
index: function() {
2011-08-07 19:33:07 +00:00
var view = this.views['_index'];
$.when(this.hideAllViews()).then(
function() { return view.show(); });
},
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(); });
}
});
2010-12-09 00:21:22 +00:00
$(document).ready(function() {
2011-08-07 19:33:07 +00:00
new BackboneStore();
Backbone.history.start();
});
2011-08-08 00:44:17 +00:00
}).call(this);