Skip to content

Instantly share code, notes, and snippets.

@yoamomonstruos
Created July 11, 2014 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoamomonstruos/6a5ce7e913c7d46b19e8 to your computer and use it in GitHub Desktop.
Save yoamomonstruos/6a5ce7e913c7d46b19e8 to your computer and use it in GitHub Desktop.
// Session.setDefault('sidebar-view-state', null);
// Session.setDefault('sidebar-drawer-state', null);
// Session.setDefault('sidebar-sub-drawer-state', null);
Carts = new Meteor.Collection('carts');
Products = new Meteor.Collection('products');
Payments = new Meteor.Collection('payments');
Meteor.methods({ });
if (Meteor.isClient) {
Carts.create = function() {
return Carts.insert({
productIds: [],
paymentIds: [],
});
}
_.extend(Template.cart, {
created: function() {
Meteor.subscribe('cart', 'jXSPZhyxRxsvnKjMv');
},
products: function() {
var cart = Carts.findOne();
if (!cart) {
return;
}
return Products.find({ _id: { $in: cart.productIds }});
},
payments: function() {
var cart = Carts.findOne();
if (!cart) {
return;
}
return Payments.find({ _id: { $in: cart.paymentIds }});
}
});
_.extend(Template.products, {
created: function() {
Meteor.subscribe('products');
},
products: function() {
return Products.find();
}
});
}
if (Meteor.isServer) {
Meteor.publish('products', function() {
return Products.find();
});
Meteor.publish('cart', function(id) {
Meteor.publishWithRelations({
handle: this,
collection: Carts,
filter: id,
mappings: [{
key: 'productIds',
collection: Products
}, {
key: 'paymentIds',
collection: Payments
}]
});
});
Meteor.startup(function () {
if (Products.find().count() === 0) {
console.log("Add Products");
var bookId = Products.insert({
name: "George Barbier",
price: 24.00
});
var coffeeId = Products.insert({
name: "Flat White",
price: 1.30
});
var printId = Products.insert({
name: "Espresso Screen Print",
price: 24.00
});
}
if (Payments.find().count() === 0) {
console.log("Add payment");
var paymentId = Payments.insert({
method: "cash",
value: 2.33
});
}
if (Carts.find().count() === 0) {
console.log("Create cart");
var cartId = Carts.insert({
productIds: [bookId, printId],
paymentIds: [paymentId]
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment