我在文件api / blueprints / count.js中创建了自定义蓝图操作计数。
我想将此操作推广到所有模型。问题是当我添加像这样的自定义蓝图操作时
Directory.GetFiles(textbox.Text, "*.mp4");
我解除了申请时遇到了这个错误:
Directory.GetFiles(textbox.Text, "*.mp4", SearchOption.AllDirectories);
我通过在配置属性
中指定模型名称来解决问题 'get /:model/count': {blueprint: 'count'}
或在地址中指定
error: count :: Ignoring attempt to bind route (/:model/count) to blueprint action (`count`), but no valid model was specified and we couldn't guess one based on the path.
指定这样的问题是我还需要为每个其他模型添加路由。有什么方法可以将这条路线概括为类似这样的东西' get /:model / count':{blueprint:' count'}。
如果我们有这个功能,那就太棒了。
请帮助。
答案 0 :(得分:3)
我们为此目的实现了自定义钩子。您可以将其添加到api/hooks
文件夹。
/**
* Adds support for count blueprint and binds :model/count route for each RESTful model.
*/
import _ from 'lodash';
import actionUtil from 'sails/lib/hooks/blueprints/actionUtil';
import pluralize from 'pluralize';
const defaultCountBlueprint = (req, res) => {
let Model = actionUtil.parseModel(req);
let countQuery = Model.count();
countQuery.then(count => res.ok({count}));
};
export default function (sails) {
return {
initialize: cb => {
let config = sails.config.blueprints;
let countFn = _.get(sails.middleware, 'blueprints.count') || defaultCountBlueprint;
sails.on('router:before', () => {
_.forEach(sails.models, model => {
let controller = sails.middleware.controllers[model.identity];
if (!controller) return;
let baseRoute = [config.prefix, model.identity].join('/');
if (config.pluralize && _.get(controller, '_config.pluralize', true)) {
baseRoute = pluralize(baseRoute);
}
let route = baseRoute + '/count';
sails.router.bind(route, countFn, null, {controller: model.identity});
});
});
cb();
}
}
};
答案 1 :(得分:0)
基于@ghaiklor的答案/代码我创建了sails可安装钩子“sails-hook-blueprint-count”来启用计数蓝图api方法。
“sails-hook-blueprint-count”包可通过npm存储库(https://www.npmjs.com/package/sails-hook-blueprint-count)获得,您可以使用
进行安装npm install sails-hook-blueprint-count
命令。
然后,当您举起帆应用程序时,您可以使用
之类的路线GET /:model/count
或
GET /:model/count?where={:criteria}
:条件与查找蓝图方法(http://sailsjs.org/documentation/reference/blueprint-api/find-where)。
的情况相同响应将是格式为
的json{count:COUNT}