A simple project to practice with AngularJS and Socket.IO.
npm install -g grunt-cli
npm install -g node-dev
git clone https://github.com/twilson63/hedgehog-practice.git
cd hedgehog-practice
npm install
grunt concat
grunt watch
node-dev app.js
open browser and editer
add search form template
public/app/tpls/index.html
<form class="form-search">
<div class="input-append">
<input class="search-query" type="text" ng-model="query">
<button class="btn" ng-click="search()">
<i class="icon-bolt"></i>
</button>
</div>
</form>
public/app/controllers/index.js
$scope.search = function() {
socket.emit('search', $scope.query, function(res) {
$scope.hogs = res.responseData.results;
});
};
/app.js
socket.on('search', function (q, fn) {
request('https://ajax.googleapis.com/ajax/services/search/images?v=1.0&' +
'q=HedgeHog+' + q,
{json: true},
function(e,r,b) { fn(b); }
);
});
public/app/tpls/index.html
<li ng-repeat="hog in hogs" >
<a href="#" ng-click="select(hog)">
<div class="media">
<div class="chevron pull-right">
<i class="icon-chevron-right"></i>
</div>
<div class="pull-left">
<img class="media-object" style="" ng-src="{{hog.tbUrl}}" />
</div>
<div class="media-body">
<h4><div ng-bind-html-unsafe="hog.title"></div></h4>
</div>
</div>
</a>
</li>
public/app/controllers/index.js
$scope.select = function(hog) {
$scope.selected = hog.url;
};
public/app/tpls/index.html
<div class="row">
<img ng-src="{{selected}}"></img>
</div>
<div class="row">
<button
style="margin-top: 20px;"
class="btn pull-right"
ng-show="selected"
ng-click="submit()">Submit</button>
</div>
public/app/controllers/index.js
$scope.submit = function() {
socket.emit('submit', $scope.selected);
$location.path('/vote');
};
/app.js
socket.on('submit', function(url) {
db.put(uuid.v1(), JSON.stringify({url: url, votes: 0}));
io.sockets.emit('addHog', url);
});
/app/tpls/vote.html
<div class="span2" ng-repeat="hog in hogs">
<a href="#" ng-click="vote(hog)">
<img
style="height: 120px; width: 120px;"
ng-src="{{hog.url}}"
/>
</a>
</div>
/public/app/controllers/vote.js
$scope.hogs = [];
socket.on('addHog', function(hog) {
$scope.hogs.push(hog);
});
socket.emit('hogs');
$scope.vote = function(hog) {
socket.emit('vote', hog);
$location.path('/leaders');
};
socket.on('vote', function(hog) {
db.put(hog.id, JSON.stringify({
url: hog.url,
votes: hog.votes + 1
}));
});
/public/app/tpls/leaders.html
<div class="span2" ng-repeat="hog in hogs | orderBy: '-votes'">
<a href="#" ng-click="vote(hog)">
<img
style="height: 120px; width: 120px;"
ng-src="{{hog.url}}"
/>
</a>
<div style="padding: 30px;font-size: 3em;font-weight: bold;">{{hog.votes}}</div>
</div>
/public/app/controllers/leaders
$scope.hogs = [];
// insert leaders code here
socket.on('addHog', function(hog) {
$scope.hogs.push(hog);
});
socket.emit('hogs');
Now you should have a simple hedgehog voting app.
If you find any errors or typos, please submit issue and I will fix
Thanks