1

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)

私の「アプリ」依存関係自体には、依存関係としてバックボーンとアンダースコアがあるため、問題はバックボーンが存在しないことではないと思います。

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()
4

5 に答える 5

1

JST問題のモジュールがロードされたときに利用可能な変数はありません。

pathsの-attribute にJST -library へのパスを追加する必要がありますrequire.config

shim次に、それをalso に追加して export にする必要がある可能性が最も高いですJST

require.js では、まだ使用されていないモジュール内の外部リソースを使用すると、警告ベルが鳴り始めるはずです。

A.defineそのモジュールのセクションにインポート

B.requireそのモジュール内でインポート

C. 機能で言及されてrequire.configいる

アップデート

変数 (例: ) を返す新しいモジュール (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

});
于 2012-08-15T13:53:05.460 に答える
1

同様の問題がありました。バックボーン ビューに次のエラーが表示されました。

Uncaught ReferenceError: 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"  
    
于 2013-03-19T20:04:45.177 に答える
0

正しい読み込み順序を指定する

require.config

require.config({
    paths: {

        // specify templates
        templates: 'compiled-templates'
    }
})

// firstly load templates
require(['templates'], function() {

    // then load you app
    require(['core/Core']);
});
于 2015-11-19T16:45:19.040 に答える
0

テンプレートをtemplates.jsという1つのファイルにコンパイルするBackbone Boilerplateを使用しています。JST は templates.js で定義されています。templates.js を使用してパスを構成すると、問題が解決されました。

于 2012-08-15T23:01:21.317 に答える
0

Jason Swettは AMD/ require.jsを使用していましたが、質問のタイトルが具体的ではないため、CommonJS/Browserify ユーザーに代わって回答すると思いました。

Backbone アプリを Browserify に移動した後、「JST in undefined error」が発生しました。上記のwawkaの回答は、物事を整理するのに役立ちました。

私が持っていた前に:

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

今私が持っています:

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

これが他の人の助けになることを願っています。

于 2014-11-03T13:59:09.903 に答える