我需要在 URL 中使用 slugs 而不是文章 ID,所以我在 meanjs 给出的文章示例中更改了一些内容,但我有一个问题,我可以列出、查看和编辑,但我无法创建新的。我不熟悉 MEAN 堆栈,所以我的修改很可能有一些非常错误的地方,但我可以想办法让它工作。
slug 是在创建文章时从标题生成的。我也想被编辑,但如果我把 slug 字段也编辑,编辑功能也会停止工作......
代码来自使用垂直模块的 meanjs 的 0.4 分支。
如果我更改,在文章.client.service.js 中:
angular.module('articles').factory('Articles', ['$resource',
function($resource) {
return $resource('api/articles/:articleSlug', {
articleSlug: '@slug'
}, {
update: {
method: 'PUT'
}
});
}
]);
为了:
angular.module('articles').factory('Articles', ['$resource',
function($resource) {
return $resource('api/articles/:articleSlug', {
articleSlug: '@_id'
}, {
update: {
method: 'PUT'
}
});
}
]);
创建功能开始工作,但编辑功能停止... -.-
任何帮助将不胜感激。谢谢
这是我的文章.server.routes.js
'use strict';
/**
* Module dependencies.
*/
var articlesPolicy = require('../policies/articles.server.policy'),
articles = require('../controllers/articles.server.controller');
module.exports = function(app) {
// Articles collection routes
app.route('/api/articles').all(articlesPolicy.isAllowed)
.get(articles.list)
.post(articles.create);
// Single article routes
app.route('/api/articles/:articleSlug').all(articlesPolicy.isAllowed)
.get(articles.read)
.put(articles.update)
.delete(articles.delete);
// Finish by binding the article middleware
app.param('articleSlug', articles.articleBySlug);
};
这是我的文章.client.service.js
'use strict';
//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
function($resource) {
return $resource('api/articles/:articleSlug', {
articleSlug: '@slug'
}, {
update: {
method: 'PUT'
}
});
}
]);
这是我的文章.client.controller.js
'use strict';
angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
function($scope, $stateParams, $location, Authentication, Articles) {
$scope.authentication = Authentication;
$scope.create = function() {
var article = new Articles({
slug: this.title.toLowerCase().replace(/ /g, '-'),
title: this.title,
content: this.content
});
article.$save(function(response) {
$location.path('articles/' + response.slug);
$scope.slug = '';
$scope.title = '';
$scope.content = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
$scope.remove = function(article) {
if (article) {
article.$remove();
for (var i in $scope.articles) {
if ($scope.articles[i] === article) {
$scope.articles.splice(i, 1);
}
}
} else {
$scope.article.$remove(function() {
$location.path('articles');
});
}
};
$scope.update = function() {
var article = $scope.article;
article.$update(function() {
$location.path('articles/' + article.slug);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
$scope.find = function() {
$scope.articles = Articles.query();
};
$scope.findOne = function() {
$scope.article = Articles.get({
articleSlug: $stateParams.articleSlug
});
};
}
]);
这是我的文章.server.controller.js
'use strict';
/**
* Module dependencies.
*/
var _ = require('lodash'),
path = require('path'),
mongoose = require('mongoose'),
Article = mongoose.model('Article'),
errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));
/**
* Create a article
*/
exports.create = function(req, res) {
var article = new Article(req.body);
article.user = req.user;
article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(article);
}
});
};
/**
* Show the current article
*/
exports.read = function(req, res) {
res.json(req.article);
};
/**
* Update a article
*/
exports.update = function(req, res) {
var article = req.article;
article.title = req.body.title;
article.content = req.body.content;
article.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(article);
}
});
};
/**
* Delete an article
*/
exports.delete = function(req, res) {
var article = req.article;
article.remove(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(article);
}
});
};
/**
* List of Articles
*/
exports.list = function(req, res) {
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.json(articles);
}
});
};
/**
* Article middleware
*/
exports.articleBySlug = function(req, res, next, slug) {
Article.findOne({'slug': slug}).populate('user', 'displayName').exec(function(err, article) {
if (err) return next(err);
if (!article) return next(new Error('Failed to load article ' + slug));
req.article = article;
next();
});
};