![](/img/trans.png)
[英]Meteor Collection is not accessible in client side when using CoffeeScript
[英]Can't create a client only collection in meteor using CoffeeScript
嗨,我需要創建一個客戶端集合,Im使用CoffeeScript,Im試圖像這樣創建它:
Template.mcqQuestionOptionsFieldset.onCreated ->
@AddMcqOptions = new Mongo.Collection null
當我運行該應用程序不起作用時,出現錯誤:
ReferenceError:未定義AddMcqOptions
我嘗試了不使用'@'符號的情況,但我嘗試了所有操作,但在javascript控制台中卻看不到已加載它,看來大學不存在...
如果刪除@符號,錯誤路徑將移至其他文件,我試圖使用該變量,但是如果添加@錯誤路徑顯示在聲明該變量的同一文件中,有人可以告訴我發生了什么嗎?
謝謝
如果要為特定於客戶端的模板設置模板,可以嘗試這樣。 我正在使用這樣的,並且工作正常:
Template.myTemplate.created = function () {
var instance = this;
instance._MyClientCollection = new Mongo.Collection(null);
instance._MyClientCollection.insert({...});
}
Template.myTemplate.helpers({
options: function () {
return Template.instance()._MyClientCollection.find({...}, {sort: {...}});
}
});
Template.myTemplate.destroyed = function () {
var instance = this;
instance._MyClientCollection.remove({});
}
希望這會有所幫助...
無需在模板中定義客戶端集合,尤其是在其他文件中使用它時。
只需添加一個文件,例如client/collections.coffee
:
@AddMcqOptions = new Mongo.Collection null
然后在客戶端上的任何地方正常使用AddMcqOptions.insert(...)
等。
如果您想保留當前結構,也可以使用粗箭頭(不過我還沒有測試過)。 您還會失去進入this
/ @
參照的模板。
Template.mcqQuestionOptionsFieldset.onCreated =>
@AddMcqOptions = new Mongo.Collection null
另一個可能的選項允許您正常使用this
/ @
:
self = this
Template.mcqQuestionOptionsFieldset.onCreated ->
self.AddMcqOptions = new Mongo.Collection null
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.