我正在尝试使用 coffeescript 在严格模式下构建一个 meteor 包。 主要问题是使用 share,如 coffeescript meteor package 中所述。 .看来我误解了链接中的解释,因为我收到以下错误:
ReferenceError: __coffeescriptShare is not defined
打包在 javascript 中运行良好。我只是将 NotificationCommon 的定义放在 'use strict' 之前。
目标是
- NotificationCommon,作用域包
- NotificationClient 和NotificationServer:作用域文件
- 通知:导出
这是我使用coffeescript 的版本:
notification_common.coffee:
'use strict'
class share.NotificationCommon
constructor: ->
@collection = new (Meteor.Collection)('notification')
@SUCCESS = 'success'
@ERROR = 'error'
@INFO = 'info'
@WARNING = 'warning'
notification_client.coffee:
'use strict'
class NotificationClient extends share.NotificationCommon
constructor = ->
self = @
Meteor.subscribe 'user_notif'
toastr.options = positionClass: 'toast-bottom-full-width'
@collection.find({}).observe 'added': (notif) ->
self.notify notif
return
notify = (notif) ->
toastr[notif.type] notif.message, notif.title, notif.options
@collection.update { _id: notif._id }, $set: notified: true
return
Notification = new NotificationClient
notification_server.coffee:
'use strict'
class NotificationServer extends share.NotificationCommon
constructor = ->
self = @
@collection.allow update: (userId, doc, fields, modifier) ->
return doc.userId is userId
Meteor.publish 'user_notif', ->
if @userId
return self.collection.find( userId: @userId, notified: false)
return
notify = (user_id, notif) ->
self = @
checkType = Match.Where((x) ->
check x, String
check x, Match.OneOf('success', 'error', 'info', 'warning')
true
)
check notif,
type: checkType
message: String
title: Match.Optional(String)
options: Match.Optional(Match.Any)
context: Match.Optional(Match.Any)
if user_id isnt null
if not Meteor.users.findOne(_id: user_id)
throw new (Meteor.Error)('User id not found.')
notif.userId = user_id
notif.notified = false
self.collection.insert notif
return
Notification = new NotificationServer
和package.js:
Package.describe({
name: 'my:notification',
version: '0.0.1',
summary: 'User and system wide notification',
git: '',
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.1.0.2');
api.use(['coffeescript','jquery']);
api.use('chrismbeckett:toastr');
api.export('Notification');
api.addFiles('notification_common.coffee');
api.addFiles('notification_server.coffee','server');
api.addFiles('notification_client.coffee','client');
});
Package.onTest(function(api) {
api.use(['tinytest','coffeescript']);
api.use('my:notification');
api.addFiles('notification-tests.js');
});
任何帮助将不胜感激。
最佳答案
解决方法是添加包 fds:coffeescript-share在 package.js 中:
api.use('fds:coffeescript-share');
查看链接以获取解释。
如果包的解释coffeescript会很有帮助将描述问题(与use strict相关)并通知上述包的存在。
编辑
谢谢@jorjordandan征求意见。 我可能会得出结论,目前还没有完整的解决方案来结合
- meteor 包
- CoffeeScript
- 严格模式
编辑 2
我已经更深入地分析了这个问题,我认为在 coffeescript 包中找不到解决方案。
该问题应该由 meteor 团队在包构建代码中解决。
我可以想象一个解决方案如下。一个新指令被添加到 Meteor 包 API 中:在 export 旁边还有一个类似 scopePackage 的指令。
使用 scopePackage 声明的变量将列在 /* Package-scope variables */
下(在串联的 js 中)。
但是它们不会被导出(代码在 /* Exports */
下,在连接的 js 中)。
关于javascript - 带有 coffeescript 的 meteor 包(以及继承和严格模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31759640/