4

是否可以即时创建新的 Meteor 收藏?我想创建foo_barbar_bar依赖于我认为应该是全局变量的一些路径名(这样我就可以在整个应用程序中访问它)。
就像是:

var prefix = window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
var Bar = new Meteor.Collection(prefix+'_bar');

这里的事情是我应该prefix从 URL 获取我的变量,所以如果我在外面声明它,if (Meteor.isClient)我会得到一个错误:ReferenceError: window is not defined. 有可能做这样的事情吗?

编辑:使用 Akshats 的第一次迭代回答我的项目 js:http ://pastie.org/6411287

4

2 回答 2

4

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.

EDIT

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
       }
    }
  });
}

Also make sure your names are javascript escaped.

于 2013-03-05T07:22:31.253 回答
3

事情变得容易多了:

var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
db.createCollection("COLLECTION_NAME", (err, res) => {
    console.log(res);
});

在您的服务器方法中运行它。

于 2015-12-30T17:21:18.147 回答