javascript - 带有 coffeescript 的 meteor 包(以及继承和严格模式)

标签 javascript meteor coffeescript package strict-mode

我正在尝试使用 coffeescript 在严格模式下构建一个 meteor 包。 主要问题是使用 share,如 coffeescript meteor package 中所述。 .看来我误解了链接中的解释,因为我收到以下错误:

 ReferenceError: __coffeescriptShare is not defined

打包在 javascript 中运行良好。我只是将 NotificationCommon 的定义放在 'use strict' 之前。

目标是

  • NotificationCommon,作用域包
  • NotificationClientNotificationServer:作用域文件
  • 通知:导出

这是我使用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-sharepackage.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/

相关文章:

javascript - Firebase 和 Crossroads 用户个人资料

javascript - 如何禁止在表单中复制/粘贴?

clojure - map 重组

javascript - 将 d3 svg 附加到当前 ng-repeat 元素

javascript - 无法在 Vue 中渲染多个组件

javascript - 如何通过 Meteor 中的#each block 传递集合信息?

node.js - 如何从 Atmosphere 编辑不在 GitHub 上的 Meteor 包?

javascript - Bootstrap DatePicker 允许通过键入进行编辑

javascript - 在构造函数中定义时未定义的类变量

javascript - 宣布数组的长度