即时创建新的 Meteor 集合 [英] Creating new Meteor collections on the fly
问题描述
是否可以即时创建新的 Meteor 收藏?我想根据某个路径名创建 foo_bar
或 bar_bar
,我想这应该是一个全局变量(这样我就可以在整个应用程序中访问它).
类似的东西:
Is it possible to create new Meteor collections on-the-fly? I'd like to create foo_bar
or bar_bar
depending on some pathname which should be a global variable I suppose (so I can access it throughout my whole application).
Something like:
var prefix = window.location.pathname.replace(/^/([^/]*).*$/, '$1');
var Bar = new Meteor.Collection(prefix+'_bar');
这里的问题是我应该从 URL 获取我的 prefix
变量,所以如果我在 if (Meteor.isClient)
之外声明它,我会收到一个错误:<代码>参考错误:窗口未定义.有可能做这样的事情吗?
The thing here is that I should get my prefix
variable from URL, so if i declare it outside of if (Meteor.isClient)
I get an error: ReferenceError: window is not defined
. Is it possible to do something like that at all?
编辑:使用 Akshats 的第一次迭代回答我的项目 js:http://pastie.org/6411287
Edit : Using the first iteration of Akshats answer my project js : http://pastie.org/6411287
推荐答案
我不完全确定这会奏效:
I'm not entirely certain this will work:
你需要把它分成两部分,第一部分加载你之前设置的集合(在客户端和服务器上)
You need it in two pieces, the first to load collections you've set up before (on both the client and server)
var collections = {};
var mysettings = new Meteor.Collection('settings') //use your settings
//Startup
Collectionlist = mysettings.find({type:'collection'});
Collectionlist.forEach(function(doc) {
collections[doc.name] = new Meteor.Collection(doc.name);
})'
并且您需要在服务器上添加集合:
And you need a bit to add the collections on the server:
Meteor.methods({
'create_server_col' : function(collectionname) {
mysettings.insert({type:'collection', name: collectionname});
newcollections[collectionname] = new Collection(collectionname);
return true;
}
});
并且您需要在客户端上创建它们:
And you need to create them on the client:
//Create the collection:
Meteor.call('create_server_col', 'My New Collection Name', function(err,result) {
if(result) {
alert("Collection made");
}
else
{
console.log(err);
}
}
同样,这一切都未经测试,所以我只是试一试,希望它能奏效.
Again, this is all untested so I'm just giving it a shot hopefully it works.
编辑
也许下面应该可以工作,我已经添加了一些检查以查看集合是否首先存在.请您先运行meteor reset
,然后再使用它从上面的代码中对错误进行排序:
Perhaps the below should work, I've added a couple of checks to see if the collection exists first. Please could you run meteor reset
before you use it to sort bugs from the code above:
var collections = {};
var mysettings = new Meteor.Collection('settings')
if (Meteor.isClient) {
Meteor.startup(function() {
Collectionlist = mysettings.find({type:'collection'});
Collectionlist.forEach(function(doc) {
eval("var "+doc.name+" = new Meteor.Collection("+doc.name+"));
});
});
Template.hello.greeting = function () {
return "Welcome to testColl.";
};
var collectionname=prompt("Enter a collection name to create:","collection name")
create_collection(collectionname);
function create_collection(name) {
Meteor.call('create_server_col', 'tempcoll', function(err,result) {
if(!err) {
if(result) {
//make sure name is safe
eval("var "+name+" = new Meteor.Collection('"+name+"'));
alert("Collection made");
console.log(result);
console.log(collections);
} else {
alert("This collection already exists");
}
}
else
{
alert("Error see console");
console.log(err);
}
});
}
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
Collectionlist = mysettings.find({type:'collection'});
Collectionlist.forEach(function(doc) {
collections[doc.name] = new Meteor.Collection(doc.name);
});
});
Meteor.methods({
'create_server_col' : function(collectionname) {
if(!mysettings.findOne({type:'collection', name: collectionname})) {
mysettings.insert({type:'collection', name: collectionname});
collections[collectionname] = new Meteor.Collection(collectionname);
return true;
}
else
{
return false; //Collection already exists
}
}
});
}
还要确保你的名字是 javascript 转义的.
Also make sure your names are javascript escaped.
这篇关于即时创建新的 Meteor 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!