Javascript 在字符串中传递_id和搜索查询
在测试项目中使用meteor。在玩示例Javascript 在字符串中传递_id和搜索查询,javascript,regex,mongodb,meteor,url-routing,Javascript,Regex,Mongodb,Meteor,Url Routing,在测试项目中使用meteor。在玩示例todo应用程序时,无法确定如何传递ID和搜索参数 目前,我的铁制路由器中有: this.route('team', { path: '/team/:_id', onBeforeAction: function() { this.todosHandle = Meteor.subscribe('todos', this.params._id); // Then filter mongoDB to sear
todo
应用程序时,无法确定如何传递ID和搜索参数
目前,我的铁制路由器中有:
this.route('team', {
path: '/team/:_id',
onBeforeAction: function() {
this.todosHandle = Meteor.subscribe('todos', this.params._id);
// Then filter mongoDB to search for the text
}});
问题是,我还想传递一个可选的search
参数来搜索todos
。所以类似于path:'/team/:_id(/search/:search)?'
有什么办法吗 在客户端,您只需使用Meteor.subscribe('todos')代码>在顶级代码中<代码>'todos'此处不引用集合,它是一个任意字符串。订阅并不关心你的路线 在服务器上,您将具有如下发布功能:
Meteor.publish('todos', function() {
if (!Meteor.userId()) return;
// return all todos (you could pass whatever query params)
return Todos({});
});
然后,在路线定义上:
Router.route('team', {
path: '/team/:_id',
data: function() {
if (this.params.query) { //if there's a query string
return Todos.find(/* according to the query string */).fetch();
else {
// return all the user's todos
return Todos.find({ uid: this.params._id }).fetch();
}
}
});
在客户机上,您只需使用
Meteor.subscribe('todos')代码>在顶级代码中<代码>'todos'
此处不引用集合,它是一个任意字符串。订阅并不关心你的路线
在服务器上,您将具有如下发布功能:
Meteor.publish('todos', function() {
if (!Meteor.userId()) return;
// return all todos (you could pass whatever query params)
return Todos({});
});
然后,在路线定义上:
Router.route('team', {
path: '/team/:_id',
data: function() {
if (this.params.query) { //if there's a query string
return Todos.find(/* according to the query string */).fetch();
else {
// return all the user's todos
return Todos.find({ uid: this.params._id }).fetch();
}
}
});
在客户机上,您只需使用
Meteor.subscribe('todos')代码>在顶级代码中<代码>'todos'
此处不引用集合,它是一个任意字符串。订阅并不关心你的路线
在服务器上,您将具有如下发布功能:
Meteor.publish('todos', function() {
if (!Meteor.userId()) return;
// return all todos (you could pass whatever query params)
return Todos({});
});
然后,在路线定义上:
Router.route('team', {
path: '/team/:_id',
data: function() {
if (this.params.query) { //if there's a query string
return Todos.find(/* according to the query string */).fetch();
else {
// return all the user's todos
return Todos.find({ uid: this.params._id }).fetch();
}
}
});
在客户机上,您只需使用
Meteor.subscribe('todos')代码>在顶级代码中<代码>'todos'
此处不引用集合,它是一个任意字符串。订阅并不关心你的路线
在服务器上,您将具有如下发布功能:
Meteor.publish('todos', function() {
if (!Meteor.userId()) return;
// return all todos (you could pass whatever query params)
return Todos({});
});
然后,在路线定义上:
Router.route('team', {
path: '/team/:_id',
data: function() {
if (this.params.query) { //if there's a query string
return Todos.find(/* according to the query string */).fetch();
else {
// return all the user's todos
return Todos.find({ uid: this.params._id }).fetch();
}
}
});
根据您的解释,听起来您希望仔细控制哪些文档实际发布到客户端,而不是发布所有文档并缩小客户端上的结果集。在这种情况下,我建议首先在服务器上定义发布,如下所示:
Meteor.publish('todosByTeamIdAndSearch', function(todoTeamId, searchParameter) {
var todosCursor = null;
// Check for teamId and searchParameter existence and set
// todosCursor accordingly. If neither exist, return an empty
// cursor, while returning a subset of documents depending on
// parameter existence.
todosCursor = Todos.find({teamId: todoTeamId, ...}); // pass parameters accordingly
return todosCursor;
});
要了解有关定义更细粒度发布的更多信息,请访问
使用上面定义的出版物,您可以按如下方式设置路线:
Router.route('/team/:_id/search/:search', {
name: 'team',
waitOn: function() {
return Meteor.subscribe('todosByTeamIdAndSearch', this.params._id, this.params.search);
},
data: function() {
if(this.ready()) {
// Access your Todos collection like you normally would
var todos = Todos.find({});
}
}
});
从示例路由定义中可以看出,您可以定义路由的路径,就像您希望在调用Router.route()
函数时直接看到的那样,并访问直接传入的参数,就像在waitOn
路由选项中一样。因为发布是按照我的建议定义的,所以您只需将这些路由参数直接传递给Meteor.subscribe()
函数即可。然后,在数据
路由选项中,检查订阅是否已准备就绪后,您可以像正常情况一样访问Todos
集合,而无需进一步缩小结果集(如果不需要这样做)
为了了解有关如何配置路由的更多信息,请查看以下两个链接:根据您的解释,您似乎希望仔细控制哪些文档实际发布到客户端,而不是发布所有文档并缩小客户端上的结果集。在这种情况下,我建议首先在服务器上定义发布,如下所示:
Meteor.publish('todosByTeamIdAndSearch', function(todoTeamId, searchParameter) {
var todosCursor = null;
// Check for teamId and searchParameter existence and set
// todosCursor accordingly. If neither exist, return an empty
// cursor, while returning a subset of documents depending on
// parameter existence.
todosCursor = Todos.find({teamId: todoTeamId, ...}); // pass parameters accordingly
return todosCursor;
});
要了解有关定义更细粒度发布的更多信息,请访问
使用上面定义的出版物,您可以按如下方式设置路线:
Router.route('/team/:_id/search/:search', {
name: 'team',
waitOn: function() {
return Meteor.subscribe('todosByTeamIdAndSearch', this.params._id, this.params.search);
},
data: function() {
if(this.ready()) {
// Access your Todos collection like you normally would
var todos = Todos.find({});
}
}
});
从示例路由定义中可以看出,您可以定义路由的路径,就像您希望在调用Router.route()
函数时直接看到的那样,并访问直接传入的参数,就像在waitOn
路由选项中一样。因为发布是按照我的建议定义的,所以您只需将这些路由参数直接传递给Meteor.subscribe()
函数即可。然后,在数据
路由选项中,检查订阅是否已准备就绪后,您可以像正常情况一样访问Todos
集合,而无需进一步缩小结果集(如果不需要这样做)
为了了解有关如何配置路由的更多信息,请查看以下两个链接:根据您的解释,您似乎希望仔细控制哪些文档实际发布到客户端,而不是发布所有文档并缩小客户端上的结果集。在这种情况下,我建议首先在服务器上定义发布,如下所示:
Meteor.publish('todosByTeamIdAndSearch', function(todoTeamId, searchParameter) {
var todosCursor = null;
// Check for teamId and searchParameter existence and set
// todosCursor accordingly. If neither exist, return an empty
// cursor, while returning a subset of documents depending on
// parameter existence.
todosCursor = Todos.find({teamId: todoTeamId, ...}); // pass parameters accordingly
return todosCursor;
});
要了解有关定义更细粒度发布的更多信息,请访问
使用上面定义的出版物,您可以按如下方式设置路线:
Router.route('/team/:_id/search/:search', {
name: 'team',
waitOn: function() {
return Meteor.subscribe('todosByTeamIdAndSearch', this.params._id, this.params.search);
},
data: function() {
if(this.ready()) {
// Access your Todos collection like you normally would
var todos = Todos.find({});
}
}
});
从示例路由定义中可以看出,您可以定义路由的路径,就像您希望在调用Router.route()
函数时直接看到的那样,并访问直接传入的参数,就像在waitOn
路由选项中一样。因为发布是按照我的建议定义的,所以您只需将这些路由参数直接传递给Meteor.subscribe()
函数即可。然后,在数据
路由选项中,检查订阅是否已准备就绪后,您可以像正常情况一样访问Todos
集合,而无需进一步缩小结果集(如果不需要这样做)
为了了解有关如何配置路由的更多信息,请查看以下两个链接:根据您的解释,您似乎希望仔细控制哪些文档实际发布到客户端,而不是发布所有文档并缩小客户端上的结果集。在这种情况下,我建议首先在服务器上定义发布,如下所示:
Meteor.publish('todosByTeamIdAndSearch', function(todoTeamId, searchParameter) {
var todosCursor = null;
// Check for teamId and searchParameter existence and set
// todosCursor accordingly. If neither exist, return an empty
// cursor, while returning a subset of documents depending on
// parameter existence.
todosCursor = Todos.find({teamId: todoTeamId, ...}); // pass parameters accordingly
return todosCursor;
});
要了解有关定义更细粒度发布的更多信息,请访问
公开发表