Last active
August 29, 2015 14:10
-
-
Save AdamBrodzinski/5b242c0332d255472cf0 to your computer and use it in GitHub Desktop.
shows data flow using models, controllers, views, and view controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// globals in /both/lib/namespaces.js | |
db = {}; | |
PostsController = {}; | |
Post = {}; | |
// use iron router controllers with custom namespace | |
// in /both/controllers/posts.js | |
// | |
// render a page to create a new post | |
PostsController.new = AppController.extend({ | |
template: 'newPost' | |
}); | |
// in /client/pages/posts/new.js | |
// | |
// template helpers are only responsible for passing data to the controller, they are faily dumb | |
Template.newPost.events(function(){ | |
'click .submit': function(e){ | |
e.preventDefault(); | |
var form = new ParseForm('#new-post-form'); | |
if (!formValid(form)) return; | |
PostsController.create({ | |
title: form.title, | |
description: form.desc | |
}); | |
} | |
}); | |
// add create controller method to update view on success/error | |
PostsController.create = function(data, callback) { | |
log('[PostsController] create', data); | |
// call Post model here... | |
Post.create(data, function(err, id){ | |
if (callback) callback(err, id); | |
if (err) return alert(err.reason); | |
analytics.track('created post') | |
Router.go('/somewhere'); | |
}); | |
}; | |
// in /both/models/posts.js | |
// located in both folder to use on either server or client | |
// use allow/deny for security | |
// | |
Post.create = function(data, callback){ | |
log('[Posts] create', data); | |
var newId = db.posts.insert(data, function(err, id){ | |
if (callback) callback(err, id); | |
if (err) { | |
trackError(err); | |
return; | |
} | |
}); | |
return newId; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment