【问题标题】:web dependencies for play asset pipeline播放资产管道的 Web 依赖项
【发布时间】:2013-03-01 21:29:15
【问题描述】:

我来自 Rails 世界,现在正在玩游戏 (2.1)。我喜欢它带有一种资产管道,但我缺少的一件事是在其中包含 Web 依赖项。在 Rails 中,我可以添加 jquery-rails 依赖项并将其包含在我的 application.js 文件中以使其可用。

我偶然发现了 http://webjars.org,这看起来不错,但据我所知,我只能将文件直接包含在 html 文件中。

有什么方法可以实现这种行为,还是计划在不久的将来实施?

【问题讨论】:

    标签: playframework-2.0 asset-pipeline webjars


    【解决方案1】:

    它的支持方式与 Rails 不同(显然),所以我想出的最好的方法如下:

    1. 包含您的 WebJars 依赖项
    2. 将 WebJars 路由定义为 documented here
    3. 将您的 JavaScript 文件放入 /app/assets/javascripts
    4. 使用 RequireJS(如 documented here)和 WebJars RequireJS 加载器来包含 WebJars 依赖项

    例如,假设您想使用 AngularJS。

    构建.scala:

    val appDependencies = Seq(
      "org.webjars" % "webjars-play" % "2.1.0-2",
      "org.webjars" % "angularjs" % "1.0.7"
    )
    

    配置/路由

    GET     /webjars/*file      controllers.WebJarAssets.at(file)
    

    app/assets/javascripts/main.js

    require(["webjars!angular.js"], function() {
      console.log(angular.isString(3));
    }
    

    app/views/example.scala.html

    ...
    @helper.requireJs(core = routes.Assets.at("javascripts/require.js").url, module = routes.Assets.at("javascripts/main").url)
    ...
    

    现在 JavaScript 控制台打印“false”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多