【问题标题】:Backbone view won't load JST主干视图不会加载 JST
【发布时间】:2015-11-20 00:45:19
【问题描述】:

我有一个使用 Backbone.js 的应用程序。一切工作正常,但最近我将 RequireJS 添加到我的项目中,这当然使一切都崩溃了,所以我正在定义我的依赖项并使一切重新工作。

我得到的错误是Uncaught ReferenceError: JST is not defined.

我有以下 CoffeeScript 视图文件。注意 JST 行:

define ["app"], (App) ->
  Snip.Views.Appointments ||= {}

  class Snip.Views.Appointments.IndexView extends Backbone.View
    template: JST["backbone/templates/appointments/index"]

    initialize: () ->
      @options.appointments.bind('reset', @addAll)

    addAll: () =>
      @options.appointments.each(@addOne)

    addOne: (appointment) =>
      view = new Snip.Views.Appointments.AppointmentView({model : appointment})
      @$("ul").append(view.render().el)

我的“应用程序”依赖项本身具有 Backbone 和 Underscore 作为依赖项,所以我认为问题不在于 Backbone 不存在:

define ["underscore", "backbone"], (_, Backbone) ->
  window.Snip =
    Models: {}
    Collections: {}
    Routers: {}
    Views: {}

当我加载页面时,我得到Uncaught ReferenceError: JST is not defined

我需要做什么才能让我的脚本了解 JST?

编辑:这是我的路径和内容

require
  paths:
    jquery: "jquery-1.7.2.min"
    underscore: "lodash.min"
    appointment: "backbone/models/appointment"
    appointmentIndexView: "backbone/views/appointments/index_view"
    appointmentsRouter: "backbone/routers/appointments_router"
    relational: "backbone-relational"
  shim:
    "underscore":
      exports: "_"
    "backbone":
      deps: ["underscore", "jquery"]
      exports: "Backbone"
    "relational":
      deps: ["backbone"]

requirejs ["appointmentsRouter"], (AppointmentsRouter) ->
  window.router = new Snip.Routers.AppointmentsRouter({appointments: []})
  Backbone.history.start()

【问题讨论】:

  • 在您定义路径的地方显示您的 require.config -function
  • 新开发:JST 显然应该被定义为一个数组,每个元素都是一个模板。 (我通过在我的 repo 中回到过去工作的时间来了解这一点。)
  • 是否愿意将此添加为答案 jason?

标签: javascript backbone.js coffeescript jst


【解决方案1】:

加载相关模块时,没有名为JST 的变量可用。

您需要将 JST 库的路径添加到 require.config 中的 paths-attribute。

那么您很可能还需要将其添加到shim 并使其导出JST

在 require.js 中,当您在模块中使用一些尚未使用的外部资源时,您的警钟应该开始响起

A.在该模块的 define 部分中导入

B.通过该模块内的require 导入

C.在你的require.config -function中提到了

更新

您需要创建一个新模块(例如 templates.js),它返回一个变量(例如 JST)。

define([
  ...
], function( ... ) {
  
  var JST = {};

  JST['template/name'] = "<div class='my-template'><%= my-content %></div>";

  ...

  return JST;
}

然后在你要使用的模块中的那些模板:

define([
  ...
  'path/to/your/JST/module',
  ...
], function(..., JST, ...) {

// Et voilà, now you can use the templates like they should be used

});

【讨论】:

  • 好的,谢谢。我感到困惑的是,这个 JST 东西以前可以正常工作,但现在不行,但是没有像“jst.js”文件或类似的文件。当我 grep JST 时,我什么也得不到。所以我不知道我需要包含什么。
  • 表达得更清楚:我想在我的路径列表中添加JST库的路径,但我不知道我的JST库会是什么。以前,我不必显式包含任何 JST 库,而且我系统上的任何文件似乎都不是 JST 库。 (如果网上有的话,也不好找。)
  • 你在哪里找到这个 JST 的东西?
  • 它代表 JavaScript 模板,我没有自己寻找,Backbone 只是使用它。
  • 仅供参考,Backbone 不只是使用它。 JST 只是您用来存储模板的对象的缩写。现在,我假设您有一个 require.js 模块,您可以在其中定义所有模板?
【解决方案2】:

我遇到了类似的问题。我的主干视图出现错误:

未捕获的引用错误:未定义 JST。

低于我为使其工作所做的工作:

  1. 在我的 config/requirejs.yml 我坐在:

    modules:
        - name: "products/product_price_tmpl"
    
  2. 我的视图定义:

    define ['jquery', 'underscore', 'backbone', 'products/product_price_tmpl'] , ($, _, Backbone) ->
    
    Backbone.View.extend
    
        template: JST['products/product_price_tmpl']
    
        tagName: 'li'
    
        render: ->
            @$el.html(@template(@model.attributes))
            @
    
  3. 我所有的模板都在 assets/templates 目录中。如果 requirejs 在这个目录下找不到你的模板,你可以将 templates 目录移动到 assets/javascripts 文件夹或者在 config/application.rb 文件中添加这一行:

    config.assets.paths << "#{Rails.root}/app/assets/templates"  
    

【讨论】:

    【解决方案3】:

    我正在使用 Backbone Boilerplate,它将模板编译成一个名为 templates.js 的文件。 JST 在 templates.js 中定义。使用 templates.js 配置路径解决了这个问题。

    【讨论】:

    • 您介意向我展示您的 templates.js 以便我看一个示例吗? (我在 Backbone Boilerplate GitHub 上很难找到这样的东西。)
    • 安装主干样板后,在命令行运行“bbb debug”,会在dist/debug/下生成templates.js文件。这是 templates.js 中的样子: this['JST'] = this['JST'] || {}; this['JST']['app/templates/example.html'] = function(data) { return function (obj,_) { var __p=[],print=function(){__p.push.apply(__p ,arguments);};with(obj||{}){__p.push(' ...}(data, _)};
    【解决方案4】:

    虽然Jason Swett 使用的是 AMD/require.js,但由于问题标题并不具体,我想我会代表 CommonJS/Browserify 用户回答,因为我在谷歌上搜索了同样的问题后最终来到了这里。

    将我的 Backbone 应用程序移至 Browserify 后,出现“未定义的 JST 错误”。 wawka上面的回答帮助我把事情整理好。

    在我有之前:

    JST["path/to/template"]({...});
    

    现在我有:

    var template = require("path/to/template");
    ...
    template({...})
    

    希望这对其他人有所帮助。

    【讨论】:

      【解决方案5】:

      指定正确的加载顺序

      require.config

      require.config({
          paths: {
      
              // specify templates
              templates: 'compiled-templates'
          }
      })
      
      // firstly load templates
      require(['templates'], function() {
      
          // then load you app
          require(['core/Core']);
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-11
        • 1970-01-01
        • 2014-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多