diff --git a/app.coffee b/app.coffee index 695ab0c..cd71aa7 100755 --- a/app.coffee +++ b/app.coffee @@ -18,6 +18,18 @@ class Home extends app.View constructor: -> super @controller = new Posts @model, @ + +class Show extends app.View + constructor: ( model, req, res, id ) -> + super + @controller = new PostsShow @model, @, id + +class PostsShow extends app.Controller + constructor: ( Post, View, id ) -> + super + Post.find( id ) + render: -> + @view.render() class Posts extends app.Controller constructor: ( Post ) -> @@ -30,6 +42,10 @@ app.get '/', ( req, res ) -> @model = new Post @view = new Home @model, req, res +app.get '/:id', ( req, res, id ) -> + @model = new Post + @view = new Show @model, req, res, id + app.post '/post/new', ( req, res ) -> @fullBody = ''; req.on 'data', (chunk) => @@ -46,4 +62,3 @@ $('#post-save').click => $( '#post-title' ).val '' $( '.modal' ).removeClass 'active' $( '.modal-bg' ).remove() - diff --git a/app.js b/app.js index 9ce6c4e..1359b10 100755 --- a/app.js +++ b/app.js @@ -1,6 +1,6 @@ // Generated by CoffeeScript 1.6.3 (function() { - var Home, Post, Posts, app, _ref, + var Home, Post, Posts, PostsShow, Show, app, _ref, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, _this = this; @@ -38,6 +38,34 @@ })(app.View); + Show = (function(_super) { + __extends(Show, _super); + + function Show(model, req, res, id) { + Show.__super__.constructor.apply(this, arguments); + this.controller = new PostsShow(this.model, this, id); + } + + return Show; + + })(app.View); + + PostsShow = (function(_super) { + __extends(PostsShow, _super); + + function PostsShow(Post, View, id) { + PostsShow.__super__.constructor.apply(this, arguments); + Post.find(id); + } + + PostsShow.prototype.render = function() { + return this.view.render(); + }; + + return PostsShow; + + })(app.Controller); + Posts = (function(_super) { __extends(Posts, _super); @@ -59,6 +87,11 @@ return this.view = new Home(this.model, req, res); }); + app.get('/:id', function(req, res, id) { + this.model = new Post; + return this.view = new Show(this.model, req, res, id); + }); + app.post('/post/new', function(req, res) { var _this = this; this.fullBody = ''; diff --git a/lib.js b/lib.js index 19f621b..cf173df 100755 --- a/lib.js +++ b/lib.js @@ -14,10 +14,19 @@ function get( path, f ) { var res = null; var a = document.createElement( 'a' ); a.href = window.location; - if ( path = a.pathname ) { - console.log( 'ROUTE = ' + a.pathname ); + if ( path == a.pathname ) { if ( isFunc( f )) f( req, res ); + } else { + var myarray = a.pathname.split(/[\/]/); + value = false; + if (!(undefined == myarray[1])) + if (isInt(myarray[1])) { + f = routes[ '/:id' + 'get' ]; + value = myarray[1]; + } + if ( isFunc( f )) + f( req, res, value ); } } @@ -104,12 +113,19 @@ Model.prototype.send = function( evt ) { } } -Model.prototype.find = function() { +Model.prototype.find = function(id) { var modelname = get_class(this).toLowerCase(); var model = this; - $.getJSON( '/'+modelname+'.json', function(data) { - model.data = data; - model.send( 'changed' ); + $.ajax({ + type: "POST", + url: '/'+modelname+'.json', + dataType: 'json', + async: false, + data: id, + success: function (data) { + model.data = data; + model.send( 'changed' ); + } }); } @@ -233,3 +249,8 @@ function is_a(obj, className){ className = className.replace(/[^\w\$_]+/, ""); return get_class(obj) === className && {function:1}[eval("typeof(".concat(className,")"))] && obj instanceof eval(className) }; + +function isInt(value){ + var er = /^[0-9]+$/; + return ( er.test(value) ) ? true : false; +} diff --git a/node_modules/zygote/index.js b/node_modules/zygote/index.js index be82191..7ca5602 100755 --- a/node_modules/zygote/index.js +++ b/node_modules/zygote/index.js @@ -171,7 +171,7 @@ Model.prototype.find = function(id) { if (isInt(id)) var query = client.query({ text: 'SELECT * FROM '+modelname+' WHERE id = $1', - values: [id.toString()] + values: [id] }); else var query = client.query({ @@ -314,21 +314,40 @@ get('/post/_home.html', function(req,res){ return file( 'views'+'/post/_home.html',res ); }); -get('/post.json', function(req,res){ +get('/post/_show.html', function(req,res){ + return file( 'views'+'/post/_show.html',res ); +}); + +post('/post.json', function(req,res){ + var fullBody = ''; + var rows = []; + req.on('data', function(chunk) { + fullBody += chunk.toString(); + }); + var id = false; var response = res; var rows = []; var client = new pg.Client('postgres://'+prefs['dbuser']+':@'+prefs['dbhost']+':'+prefs['dbport'].toString()+'/'+prefs['dbname']); client.on('drain', client.end.bind(client)); client.connect(); - var query = client.query({ - text: 'SELECT * FROM post', - values: [] - }); - query.on('row', function(row) { - rows.push(row); - }); - query.on('end', function(result) { - return response.end(JSON.stringify(rows)); + req.on('end', function() { + id = fullBody; + if (isInt(id)) + var query = client.query({ + text: 'SELECT * FROM '+modelname+' WHERE id = $1', + values: [id] + }); + else + var query = client.query({ + text: 'SELECT * FROM '+modelname, + values: [] + }); + query.on('row', function(row) { + rows.push(row); + }); + query.on('end', function(result) { + return response.end(JSON.stringify(rows)); + }); }); });