billywhizz
7/10/2011 - 6:39 PM

nesting.js

	function handleFailure(err) {
		next(new DbError(err)); 
	}
	function findBlogCb(blog) {
		var proj;
		
		function setBlogCb() {
			proj.setUsers(users).on("success", setUsersCb).on("failure", handleFailure); 
		}

		function setUsersCb() {
			// do stuff 
		}

		function createProjectCb(project) {
			proj = project;
			project.setBlog(blog).on("success", setBlogCb).on("failure", handleFailure); 
		}

		function findUsersCb(users) {
			if (!users) return res.send({ errors: "Users do not exist."});
			Project.create(req.form.project).on("success", createProjectCb).on("failure", handleFailure); 
		}
		if (!blog) return res.send({ errors: "Blog does not exist."}); 
		User.findAll(req.form.users).on("success", findUsersCb).on("failure", handleFailure); 
	}
	Blog.find(req.form.blog).on("success", findBlogCb).on("failure", handleFailure);