Permalink
Browse files

ajax demo++

  • Loading branch information...
billy
billy committed Oct 19, 2009
1 parent 164043d commit 3067caa2b5dfaa463d168b668acded8529f593fb
Showing with 160 additions and 9 deletions.
  1. +2 −1 Blog.cfc
  2. +33 −0 BlogFacade.cfc
  3. +33 −1 MongoBlogTest.cfc
  4. +16 −7 MongoDB.cfc
  5. +76 −0 demo.cfm
View
@@ -1,10 +1,11 @@
<cfcomponent output="false">
<cfscript>
+this.id = '';
this.title = '';
this.body = '';
this.tags = [];
this.author = '';
-this.pub_data = now();
+this.pub_date = now();
this.comments = [
comment= {
comment_author = 'Coward',
View
@@ -0,0 +1,33 @@
+<cfcomponent output="false">
+<cfscript>
+mongo = createObject('component','MongoDB');
+</cfscript>
+
+
+<cffunction name="createBlogPost" access="remote">
+ <cfargument name="title" />
+ <cfargument name="author" />
+ <cfargument name="tags" />
+ <cfargument name="text" />
+ <cfscript>
+ blog = createObject('component','Blog');
+ blog.title = arguments.title;
+ blog.author = arguments.author;
+ blog.tags = listToArray(arguments.tags);
+ blog.text = arguments.text;
+ mongo.put(blog);
+ </cfscript>
+
+</cffunction>
+
+
+<cffunction name="getBlogPosts" access="remote">
+ <cfreturn mongo.find() />
+</cffunction>
+
+<cffunction name="deletePost" access="remote">
+ <cfargument name="id" type="string" />
+ <cfset mongo.deleteById(id) />
+</cffunction>
+
+</cfcomponent>
View
@@ -1,6 +1,38 @@
<cfcomponent output="false" extends="mxunit.framework.TestCase">
<cfscript>
mongo = createObject('component', 'MongoDB');
+
+ blog = createObject('component', 'Blog');
+
+ blog.title = 'Title 1';
+ blog.text = 'Text 1';
+ blog.author = 'bill-1';
+ mongo.put(blog);
+
+ blog.title = 'Title 2';
+ blog.text = 'Text 2';
+ blog.author = 'bill-2';
+ mongo.put(blog);
+
+ blog.title = 'Title 3';
+ blog.text = 'Text 3';
+ blog.author = 'bill-3';
+ mongo.put(blog);
+
+ /* */
+
+
+ function testFindIterator(){
+ items = mongo.find();
+ debug(items)
+ for(item in items){
+ post = items[item];
+ debug(post["_id"].toString());
+ }
+
+
+ }
+
function testBlog() {
@@ -27,7 +59,7 @@
// blog.author = 'ed';
// mongo.update(blog);
- // mongo.delete(blog);
+ mongo.delete(blog);
bb = mongo.findOne();
debug(bb);
View
@@ -1,14 +1,13 @@
<cfcomponent output="false">
-<!--- Maybe should extend an Abstract Mongo? --->
<cfscript>
-//And this is configurable.
+//This is configurable.
server_name = 'localhost';
server_port = 27017;
db_name = 'default_db';
collection_name = 'default_collection';
-//maybe this goes in super class
+//maybe this goes in super class?
mongo = createObject('java', 'com.mongodb.Mongo').init( variables.server_name , variables.server_port );
db = mongo.getDb(db_name);
collection = db.getCollection(collection_name);
@@ -19,6 +18,7 @@ function add(key,value,o){
//add key value pair to object ... todo
}
+
function put(o){
var doc = createObject('java', 'com.mongodb.BasicDBObject').init();
var id = chr(0);
@@ -42,12 +42,15 @@ function count(){
return collection.getCount();
}
+
function findOne(){
return collection.findOne();
} //
-function find(){
+function find(){
+ var sort_spec = createObject('java', 'com.mongodb.BasicDBObject').init({pub_date :-1});;
+ return collection.find().sort( sort_spec ).toArray();
} //end function
@@ -57,6 +60,15 @@ function delete(o){
} //end function
+//Note the ObjectId object. This creates an ObjectId from
+//the string representation of
+
+function deleteById(id){
+ var objId = createObject('java','com.mongodb.ObjectId').init(id);
+ var obj = get("_id", objId);
+ return collection.remove(obj); //id's of deleted items
+} //en
+
function update(o){
var obj = get("_id", o._id);
@@ -74,7 +86,4 @@ function switchCollection(collection_name){
</cfscript>
-<cffunction name="dump">
- <cfdump var="#doc#">
-</cffunction>
</cfcomponent>
View
@@ -0,0 +1,76 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<title>MongoDB/ColdFusion Ajax Demo</title>
+ <link type="text/css" rel="stylesheet" href="http://jqueryui.com/themes/base/ui.all.css" />
+
+<script src="http://www.google.com/jsapi"></script>
+<script>
+ // Load jQuery
+ google.load("jquery", "1.3.2");
+ google.load("jqueryui", "1.7.2");
+ google.setOnLoadCallback(function() {
+ $(document).ready(function(){
+ $('#btnPost').bind('click',function(){
+
+ $.post('BlogFacade.cfc?method=createBlogPost',
+ {title : $('input[name=title]').val(),
+ author : $('input[name=author]').val(),
+ tags : $('input[name=tags]').val(),
+ text : $('textarea[name=text]').val()
+ },
+ null, "json");
+
+ //$('#btnClear').click();
+ location.href = 'demo.cfm';
+ });
+
+ $('#btnClear').bind('click',function(){
+ $('input[name=title]').val('');
+ $('input[name=author]').val('');
+ $('input[name=tags]').val('');
+ $('textarea[name=text]').val('');
+ });
+
+ });
+
+ $('.del').bind('click',function(){
+ //alert(this.id);
+ $.post('BlogFacade.cfc?method=deletePost', {id:this.id}, null, "json");
+ $('#p_'+this.id).html('');
+ });
+
+ });
+
+</script>
+</head>
+<body>
+<h2>MongoDB/ColdFusion Ajax Demo</h2>
+<form>
+Blog Title: <input type="text" name="title"><br />
+Author: <input type="text" name="author"><br />
+Tags:<input type="text" name="tags"><br />
+Post:<br /><textarea cols="50" rows="10" name="text"></textarea><br />
+<input type="button" id="btnPost" value="Create Blog Post">
+<input type="button" id="btnClear" value="Clear">
+</form>
+<p><hr/></p>
+<h3>Posts</h3>
+<cfset mongo = createObject('component','MongoDB') />
+<cfscript>
+ //out = writeoutput;
+ items = mongo.find();
+ for(item in items){
+ post = items[item];
+ id = post["_id"].toString();
+ writeoutput( '<div id="p_#id#"><strong>' & post.title & '</strong> <em>by ');
+ writeoutput(post.author & '</em> on #dateFormat(post['PUB_DATE'],'mm/dd/yy')# #timeFormat(post['PUB_DATE'],'hh:mm:ss')# <br />');
+ writeoutput(post.text & '<br />');
+ writeoutput('<a href="javascript:void(0)" id="#id#" class="del">Delete</a>')
+ writeoutput('<p><hr size="1" noshade /></p></div>');
+ }
+</cfscript>
+
+
+
+</body>
+</html>

0 comments on commit 3067caa

Please sign in to comment.