Permalink
Browse files

updates

  • Loading branch information...
1 parent 8b3e196 commit 336ad9ce15672ba445e49910a402c492e7823015 @rjrodger committed Mar 11, 2013
View
@@ -18,6 +18,8 @@
"author": "Richard Rodger",
"license": "BSD",
"dependencies": {
- "seneca": "~0.5.0"
+ "seneca": "~0.5.2",
+ "seneca-cart": "~0.1.1",
+ "seneca-engage": "~0.1.1"
}
}
View
@@ -0,0 +1,13 @@
+
+Run with:
+
+node app.js -p 3000
+
+and then visit:
+http://localhost:3000
+
+
+Access the admin panel on:
+http://localhost:3000/admin
+
+
View
@@ -44,6 +44,9 @@ app.use(express.static(__dirname + '/public'))
// the seneca.service method returns a single function with the signature
// function(req,res,next) that can be used with connect or express
// this service method wraps up all the plugin HTTP endpoints
+// seneca includes the connect utility plugin by default, which
+// sets the special arguments req$ and res$ on all seneca calls, allowing
+// seneca actions to access the current HTTP req and res objects
app.use( seneca.service() )
@@ -53,34 +56,44 @@ app.set('views', __dirname + '/views')
app.set('view engine','ejs')
-// create a "pin" on the cart plugin, for convenience
-// the cmd's become methods on the cart object
-var cart = seneca.pin({role:'cart',cmd:'*'})
+// create somes "pins" for convenience
+// the patterns generate objects with methods matching the wildcards
+var cart_pin = seneca.pin({role:'cart',cmd:'*'})
+var engage_pin = seneca.pin({role:'engage',cmd:'*'})
+
+
+// the engage plugin can wrap calls to other actions
+// any matching argument keys are given values from the engagement store
+// in this case, the cart id (if it exists) for the current user is taken from
+// the store and used to set the value of the cart argument key
+engage_pin.wrap( {pin:{role:'cart',cmd:'*'}, keys:['cart']} )
+
// a utility method
function formatprice(price) {
return '$' + (void 0 == price ? '0.00' : price.toFixed(2))
}
-app.get('/', function(req, res){
- cart.table({req$:req,res$:res,create:false},function(err,table){
+app.get('/', function(req, res, next){
+ req.seneca.act('role:cart,cmd:get',function(err,out) {
if( err ) return next(err);
- res.render('index.ejs',{locals:{table:table,formatprice:formatprice}})
+ res.render('index.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
+
app.get('/cart', function(req,res,next){
- cart.table({req$:req,res$:res,create:true},function(err,table){
+ req.seneca.act('role:cart,cmd:get',function(err,out) {
if( err ) return next(err);
- res.render('cart.ejs',{locals:{table:table,formatprice:formatprice}})
+ res.render('cart.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
app.get('/checkout', function(req,res,next){
- cart.table({req$:req,res$:res},function(err,table){
+ req.seneca.act('role:cart,cmd:get',function(err,out) {
if( err ) return next(err);
- res.render('checkout.ejs',{locals:{table:table,formatprice:formatprice}})
+ res.render('checkout.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
@@ -95,6 +108,7 @@ server.listen(conf.port)
seneca.use('admin',{server:server,local:true})
+
// set up some test products for the store
// create a product entity object
var product_ent = seneca.make$('shop','product')
@@ -15,9 +15,11 @@
"optimist": "~0.3.5",
"ejs": "~0.8.3",
"ejs-locals": "~1.0.2",
- "seneca": "~0.5.0",
+ "seneca": "~0.5.2",
"seneca-user": "~0.2.0",
- "seneca-auth": "~0.2.0"
+ "seneca-auth": "~0.2.0",
+ "seneca-cart": "~0.1.1",
+ "seneca-engage": "~0.1.1"
},
"files": [
"app.js"
@@ -20,7 +20,6 @@ $(function(){
}
$('td.remove').click(function(){
- console.log('remove')
var entry = $(this).parent().data('entry')
console.log(entry)
entry_input.val(entry)
@@ -2,14 +2,14 @@
<h1>Cart</h1>
-<% if( !table ) { %>
+<% if( !cart ) { %>
Nothing in your cart.
<% } else { %>
<table>
-<% var entries = table.entries || []
+<% var entries = cart.entries || []
for( var i = 0; i < entries.length; i++ ) {
var entry = entries[i]
%>
@@ -23,7 +23,7 @@ Nothing in your cart.
<tr>
<th>Total</th>
-<th align="right"><%-formatprice(table.total)%></th>
+<th align="right"><%-formatprice(cart.total)%></th>
</tr>
</table>
@@ -37,6 +37,6 @@ Nothing in your cart.
<a href="/">buy more!</a>
-<form id="cart_form" method="post" action="/api/cart/remove">
+<form id="cart_form" method="post" action="/api/cart/remove_entry">
<input type="hidden" id="entry_input" name="entry">
</form>
@@ -2,15 +2,15 @@
<h1>Checkout</h1>
-<% if( !table ) { %>
+<% if( !cart ) { %>
Nothing in your cart.
<a href="/">buy something</a>
<% } else { %>
<table>
-<% var entries = table.entries || []
+<% var entries = cart.entries || []
for( var i = 0; i < entries.length; i++ ) {
var entry = entries[i]
%>
@@ -22,7 +22,7 @@ Nothing in your cart.
<tr>
<th>Total</th>
-<th align="right"><%-table.total%></th>
+<th align="right"><%-cart.total%></th>
</tr>
</table>
@@ -5,9 +5,9 @@
<div id="app01" class="buy">Buy an Apple!</div>
<div id="ora02" class="buy">Buy an Orange!</div>
-<form id="prod_form" method="post" action="/api/cart/add">
+<form id="prod_form" method="post" action="/api/cart/add_entry">
<input type="hidden" id="code_input" name="code">
</form>
<br />
-<a href="/cart">your cart (<%=table ? table.entries.length : 0%> items)</a>
+<a href="/cart">your cart (<%=cart ? cart.entries.length : 0%> items)</a>
View
@@ -0,0 +1,13 @@
+
+Run with:
+
+node app.js -p 3000
+
+and then visit:
+http://localhost:3000
+
+
+Access the admin panel on:
+http://localhost:3000/admin
+
+

0 comments on commit 336ad9c

Please sign in to comment.