Skip to content

Commit

Permalink
renders one post, client & server
Browse files Browse the repository at this point in the history
  • Loading branch information
voitto committed Jun 22, 2013
1 parent 668a724 commit e5b30d4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 19 deletions.
17 changes: 16 additions & 1 deletion app.coffee
Expand Up @@ -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 ) ->
Expand All @@ -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) =>
Expand All @@ -46,4 +62,3 @@ $('#post-save').click =>
$( '#post-title' ).val ''
$( '.modal' ).removeClass 'active'
$( '.modal-bg' ).remove()

35 changes: 34 additions & 1 deletion app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions lib.js
Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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' );
}
});
}

Expand Down Expand Up @@ -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;
}
41 changes: 30 additions & 11 deletions node_modules/zygote/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5b30d4

Please sign in to comment.