Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
Adding Bower Support
Browse files Browse the repository at this point in the history
  • Loading branch information
amoshaviv committed Jul 9, 2013
1 parent 22ce12e commit 80be397
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"directory": "public/lib",
"json": "bower.json"
}
80 changes: 80 additions & 0 deletions app/controllers/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Module dependencies.
*/

var mongoose = require('mongoose')
, async = require('async')
, Article = mongoose.model('Article')
, _ = require('underscore')


/**
* Find article by id
*/

exports.article = function(req, res, next, id){
var User = mongoose.model('User')

Article.load(id, function (err, article) {
if (err) return next(err)
if (!article) return next(new Error('Failed to load article ' + id))
req.article = article
next()
})
}

/**
* Create a article
*/
exports.create = function (req, res) {
var article = new Article(req.body)
article.user = req.user
article.save()
res.jsonp(article)
}

/**
* Update a article
*/
exports.update = function(req, res){
var article = req.article
article = _.extend(article, req.body)

article.save(function(err) {
res.jsonp(article)
})
}

/**
* Delete an article
*/
exports.destroy = function(req, res){
var article = req.article
article.remove(function(err){
if (err) {
res.render('error', {status: 500});
} else {
res.jsonp(article);
}
})
}

/**
* Show an article
*/
exports.show = function(req, res){
res.jsonp(req.article);
}

/**
* List of Articles
*/
exports.all = function(req, res){
Article.find().sort('-created').populate('user').exec(function(err, articles) {
if (err) {
res.render('error', {status: 500});
} else {
res.jsonp(articles);
}
});
}
31 changes: 31 additions & 0 deletions app/models/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose')
, env = process.env.NODE_ENV || 'development'
, config = require('../../config/config')[env]
, Schema = mongoose.Schema

/**
* Article Schema
*/

var ArticleSchema = new Schema({
created: {type : Date, default : Date.now},
title: {type: String, default: '', trim : true},
content: {type: String, default: '', trim : true},
user: {type : Schema.ObjectId, ref : 'User'}
});


/**
* Statics
*/

ArticleSchema.statics = {
load: function (id, cb) {
this.findOne({ _id : id }).populate('user').exec(cb);
}
}

mongoose.model('Article', ArticleSchema)
17 changes: 17 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "angularJS-IL",
"version": "1.0.0",
"dependencies": {
"bootstrap": "2.3.2",
"angular": "~1.0.6",
"angular-resource": "~1.0.6",
"angular-cookies": "~1.0.6",
"angular-bootstrap": "~0.4.0",
"json3": "~3.2.4",
"jquery": "~1.9.1"
},
"devDependencies": {
"angular-mocks": "~1.0.5",
"angular-scenario": "~1.0.5"
}
}
48 changes: 48 additions & 0 deletions public/js/controllers/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function ArticlesController($scope, $routeParams, $location, Articles) {
$scope.articles = [];
$scope.article = {};

$scope.create = function () {
var article = new Articles({ title: this.title, content: this.content });
article.$save(function (response) {
$location.path("articles/" + response._id);
});

this.title = "";
this.content = "";
};

$scope.remove = function (article) {
article.$remove();

for (var i in $scope.articles) {
if ($scope.articles[i] == article) {
$scope.articles.splice(i, 1)
}
}
};

$scope.update = function () {
var article = $scope.article;
if (!article.updated) {
article.updated = [];
}
article.updated.push(new Date().getTime());

article.$update(function () {
$location.path('articles/' + article._id);
});
};

$scope.find = function (query) {
Articles.query(query, function (articles) {
$scope.articles = articles;
});
};

$scope.findOne = function () {
Articles.get({ articleId: $routeParams.articleId }, function (article) {
$scope.article = article;
});
};
}
4 changes: 4 additions & 0 deletions public/js/services/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//Articles service used for articles REST endpoint
window.app.factory("Articles", function($resource){
return $resource('articles/:articleId', {articleId:'@_id'}, {update: {method: 'PUT'}});
});
21 changes: 21 additions & 0 deletions public/views/articles/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<section data-ng-controller="ArticlesController">
<form class="form-horizontal" data-ng-submit="create()">
<div class="control-group">
<label class="control-label" for="title">כותרת</label>
<div class="controls">
<input type="text" data-ng-model="title" id="title" placeholder="כותרת">
</div>
</div>
<div class="control-group">
<label class="control-label" for="content">תוכן</label>
<div class="controls">
<textarea data-ng-model="content" id="content" cols="30" rows="10" placeholder="תוכן"></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" class="btn">
</div>
</div>
</form>
</section>
25 changes: 25 additions & 0 deletions public/views/articles/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<section data-ng-controller="ArticlesController" data-ng-init="findOne()">
<form class="form-horizontal" data-ng-submit="update()">
<div class="control-group">
<label class="control-label" for="title">Title</label>

<div class="controls">
<input type="text" data-ng-model="article.title" id="title" placeholder="Title">
</input>
</div>
</div>
<div class="control-group">
<label class="control-label" for="content">Content</label>

<div class="controls">
<textarea data-ng-model="article.content" id="content" cols="30" rows="10" placeholder="Content">
</textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" class="btn">
</div>
</div>
</form>
</section>
10 changes: 10 additions & 0 deletions public/views/articles/list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<section data-ng-controller="ArticlesController" data-ng-init="find()">
<ul>
<li data-ng-repeat="article in articles">
<span>{{article.created | date:'medium'}}</span> /
<span>{{article.user.name}}</span>
<h2><a data-ng-href="#!/articles/{{article._id}}">{{article.title}}</a></h2>
<div>{{article.content}}</div>
</li>
</ul>
</section>
6 changes: 6 additions & 0 deletions public/views/articles/view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<section data-ng-controller="ArticlesController" data-ng-init="findOne()">
<span>{{article.created | date:'medium'}}</span> /
<span>{{article.user.name}}</span>
<h2>{{article.title}}</h2>
<div>{{article.content}}</div>
</section>

0 comments on commit 80be397

Please sign in to comment.