A simple project to practice with AngularJS and Socket.IO.
- NodeJS - http://nodejs.org
- Grunt -
npm install -g grunt-cli
- Node-Dev =
npm install -g node-dev
- Clone the Repo
git clone https://github.com/twilson63/hedgehog-practice.git
- cd into app
cd hedgehog-practice
- npm install
npm install
- run grunt
grunt concat
- run grunt watch
grunt watch
- run node-dev in new terminal
node-dev app.js
-
open browser and editer
-
add search form template
- Open
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>
- add search form client code
- Open
public/app/controllers/index.js
$scope.search = function() {
socket.emit('search', $scope.query, function(res) {
$scope.hogs = res.responseData.results;
});
};
- add search form server code
- Open
/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); }
);
});
- add results and select template
- Open
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>
- Add select client code
- Open
public/app/controllers/index.js
$scope.select = function(hog) {
$scope.selected = hog.url;
};
- Add submit template
- Open
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>
- Add submit client code
- Open
public/app/controllers/index.js
$scope.submit = function() {
socket.emit('submit', $scope.selected);
$location.path('/vote');
};
- Add submit server code
- Open
/app.js
socket.on('submit', function(url) {
db.put(uuid.v1(), JSON.stringify({url: url, votes: 0}));
io.sockets.emit('addHog', url);
});
- Add vote template
- Open
/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>
- Add Vote Client Code
- open
/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');
};
- Add vote server side code
socket.on('vote', function(hog) {
db.put(hog.id, JSON.stringify({
url: hog.url,
votes: hog.votes + 1
}));
});
- Add leaders template
- Open
/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>
- Add leaders client code
- open
/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