-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var gh = require('grasshopper'), | ||
Item = require('./item').Item; | ||
|
||
gh.get('/', function() { | ||
this.render('search'); | ||
}); | ||
|
||
gh.get('/search', function() { | ||
this.model['items'] = this.repo.search(this.params['tags']); | ||
this.model['searchTags'] = this.params['tags']; | ||
this.render('results'); | ||
}); | ||
|
||
gh.post('/watch', function() { | ||
var self = this; | ||
this.repo.watch(this.params['tags'], function(item) { | ||
self.renderText(item.name()); | ||
}); | ||
}); | ||
|
||
gh.get('/items/add', function() { | ||
this.model['item'] = new Item(); | ||
this.render('add'); | ||
}); | ||
|
||
gh.post('/items', function() { | ||
var item = new Item().update(this.params['item']); | ||
if(item.isValid()) { | ||
this.repo.add(item); | ||
this.redirect('/'); | ||
} else { | ||
this.model['item'] = item; | ||
this.render('add'); | ||
} | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var gh = require('grasshopper'); | ||
|
||
function Item() { | ||
}; | ||
|
||
Item.prototype.validate = function() { | ||
this.validateRequired('name'); | ||
this.validateRequired('tags'); | ||
this.validatePattern('tags', /[^\s,]/); | ||
}; | ||
|
||
exports.Item = gh.initModel(Item, 'name', 'tags'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
var EventEmitter = require('events').EventEmitter, | ||
sys = require('sys'); | ||
|
||
function ItemRepo() { | ||
EventEmitter.call(this); | ||
this.items = {}; | ||
} | ||
|
||
sys.inherits(ItemRepo, EventEmitter); | ||
|
||
ItemRepo.prototype.add = function(item) { | ||
var self = this; | ||
|
||
forEachTag(item.tags(), function(tag) { | ||
if(self.items[tag]) { | ||
self.items[tag].push(item); | ||
} else { | ||
self.items[tag] = [item]; | ||
} | ||
|
||
self.emit(tag, item); | ||
self.removeAllListeners(tag); | ||
}); | ||
}; | ||
|
||
ItemRepo.prototype.search = function(tags) { | ||
var items = [], self = this; | ||
forEachTag(tags, function(tag) { | ||
if(self.items[tag]) | ||
items = items.concat(self.items[tag]); | ||
}); | ||
return items; | ||
}; | ||
|
||
ItemRepo.prototype.watch = function(tags, cb) { | ||
var self = this; | ||
forEachTag(tags, function(tag) { | ||
self.on(tag, cb); | ||
}); | ||
}; | ||
|
||
function forEachTag(tagString, cb) { | ||
tagString.split(',').forEach(function (tag) { | ||
var trimmedTag = tag.trim(); | ||
if(trimmedTag.length > 0) | ||
cb(tag.trim()); | ||
}); | ||
} | ||
|
||
exports.ItemRepo = ItemRepo; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var gh = require('grasshopper'), | ||
ItemRepo = require('./app/itemRepo').ItemRepo; | ||
|
||
gh.configure({ | ||
viewsDir: './views', | ||
staticsDir: './statics', | ||
layout: 'layout', | ||
locales: require('./locales').locales | ||
}); | ||
|
||
gh.addToContext({ | ||
repo: new ItemRepo() | ||
}); | ||
|
||
require('./app/controllers'); | ||
|
||
gh.serve(8080); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<title>Real-time Search</title> | ||
</head> | ||
<body> | ||
<div id="content"> | ||
<%= include(view) %> | ||
</div> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
exports.locales = { | ||
'en-us': { | ||
'Item.name.required': 'Give a name for the item.', | ||
'Item.tags.required': 'Specify atleast one tag for the item.', | ||
'Item.tags.pattern': 'Tag specification is invalid.' | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function watch(tags) { | ||
$.ajax({ | ||
url: '/watch', | ||
data: {tags: tags}, | ||
type: 'POST', | ||
dataType: 'text', | ||
success: function(item, status) { | ||
watch(tags); | ||
if(item != '') { | ||
$('#itemsList').prepend($('<li></li>').text(item)); | ||
} | ||
} | ||
}); | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<h1>Add an Item</h1> | ||
<form action="/items" method="post"> | ||
Name: <input name="item.name" value="<%= item.name() %>" /> | ||
<span style="color: red"><%= error(item, 'name', locale) %></span> | ||
<p /> | ||
Tags: <input name="item.tags" value="<%= item.tags() %>" /> | ||
<span style="color: red"><%= error(item, 'tags', locale) %></span> | ||
<p /> | ||
<input type="submit" value="Save" /> | ||
<a href="/">Back</a> | ||
</form> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<h1>Real-time Results for "<%= searchTags %>"</h1> | ||
<ul id="itemsList"> | ||
<% items.forEach(function(item) { %> | ||
<li><%= h(item.name()) %></li> | ||
<% }); %> | ||
</ul> | ||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | ||
<script type="text/javascript" src="js/script.js"></script> | ||
<script type="text/javascript"> | ||
setTimeout(function() { | ||
watch('<%= searchTags %>'); | ||
}, 0); | ||
</script> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1>Real-time Search</h1> | ||
<form action="/search" method="get"> | ||
<input name="tags" /> | ||
<input type="submit" value="Search" /> | ||
<a href="/items/add">Add Item</a> | ||
</form> | ||
|