Feathers.js - 从服务实例获取服务路径/路由

时间:2018-03-07 11:49:59

标签: feathersjs

有没有办法从服务包装器获取服务路径/路由?

这样的事情:

let myService = app.service('users');

myService.name === 'users'; // true

我正在尝试基于现有服务动态创建新服务,如下所示:

let services = [service1, service2, service3];

services.forEach(service=>{
    app.use(`somePrefix-${service.name}`, {get: get, find:find /*etc*/});
});

我需要服务的路径来注册新路径。

1 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点。首先,您已经回答过:使用setup方法并设置context.Users .Select(u => new UserViewModel { UserId = u.UserId, UserName = u.Name, UserEmail = u.Email, UserType = u.UserType.Value }).ToList();

另一个是添加service mixin,只要注册了服务,就会调用它。

<html>
<head>
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Expires" content="-1">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
	<img id="viewer" src="" width="100%"/>
	<div id="info"> </div>
	<button id="btnLeft" type="button">Left</button>
	<button id="btnRight" type="button">Right</button>
	<input type="button" value="Refresh page" onclick="location.reload(true);" />

<script>
$(function() {
	var baseUrl = "./Cam01/";
	var pictureIndex = 0;
	var pictures = [];
	function getFiles() {
		$.ajax(baseUrl).success(function(data) {
			pictures = [];
			$(data).find("a[href]").each(function() {
				var href = $(this).attr('href');
				if (href.indexOf('.jpg') > 0 || href.indexOf('.png') > 0 || href.indexOf('.jpeg') > 0) {
					pictures.push(href);
				}
			});
			console.log(pictures.length + " pictures loaded!");
			changePicture(0);
		});
	}
	function changePicture(indexOffset) {
		pictureIndex += indexOffset;
		if (pictureIndex >= pictures.length) {
			pictureIndex = 0;
		} else if (pictureIndex < 0) {
			pictureIndex = pictures.length - 1;
		}
		$('#viewer').attr('src', baseUrl + pictures[pictureIndex]);
		$('#info').text((pictureIndex + 1) + "/" + pictures.length);
	}

	getFiles();
	
	$('#btnLeft').click(function() {
	var left = -1;
    changePicture(left); return false;
	});
	$('#btnRight').click(function() {
	var right = 1;
    changePicture(right); return false;
	});
	
	$(document).keydown(function(e){
		var left = -1, right = 1;
	    if (e.keyCode == 37) {
	       changePicture(left); return false;
	    } else if (e.keyCode == 39) {
	    	changePicture(right); return false;
	    }
	});
	
});
</script>
</body>
</html>