2016-11-21 8 views
0

を使用してコンテキストのためにロードされていないと私はエラーがあるモジュール名は、私は、テンプレートをロードするために<a href="https://github.com/requirejs/text" rel="nofollow noreferrer">text.js</a>を使用していRequireJSテキストプラグイン

require('text!templates/categories.html') 

上のエラーを取得しています

Uncaught Error: Module name "text!templates/categories.html_unnormalized2" has not been loaded yet for context: 

てきましたエラーがスローされる場所のテンプレートのロードプロセスが必要になったときに、私のビューセクションを見てください。

プロジェクトのディレクトリ構造:

js 
    /app 
     .... 
     views/categories 
     templates/categories 
    /lib 
     /jquery 
     /underscore 
     /backbone 
     /text 
    /vendor 
     /jquery-ui 
     /fancytree 

RequireJSのコンフィグ:

require.config({ 
    // The shim config allows us to configure dependencies for 
    // scripts that do not call define() to register a module 

    shim: { 

     underscore: { 
      exports: '_' 
     }, 
     backbone: { 
      deps: [ 
       'underscore', 
       'jquery' 
      ], 
      exports: 'Backbone' 
     }, 
     'jquery-ui': { 
      exports: "$", 
      deps: ['jquery'] 
     }, 
     'fancytree': { 
      deps: ['jquery-ui'] 
     }, 

    }, 
    paths: { 
     'jquery': '../lib/jquery/jquery', 
     'underscore': '../lib/underscore/underscore', 
     'backbone': '../lib/backbone/backbone',  
     'text': '../lib/text/text', 
     'jquery-ui': '../vendor/jquery-ui/jquery-ui', 
     'fancytree': [  
      '../vendor/fancytree/fancytree', 
      '../vendor/fancytree/fancytree.table' 
     ],  
    }, 

    baseUrl: '/js/app', 

}); 

ビュー:あなたはCommonJSの構文を使用してモジュールを必要としようとしている

define(['jquery-ui', 'fancytree', 'require'], function(ui, fancytree, require){ 

    'use strict'; 

    var $ = require('jquery'), 
     _ = require('underscore'), 
     Backbone = require('backbone'), 
     tpl = require('text!templates/categories.html') /* this line here produces error loading text.js*/, 
     template = _.template(tpl); 


    return Backbone.View.extend({ 
     el: $('#tree'), 
     initialize: function() { 

      this.listenTo(this.collection, 'reset add change remove', this.render, this); 
      this.collection.fetch(); 
     }, 
     initFancyTree: function() { 
      console.log('tree'); 
      $('#fancytree').fancytree(); 

     }, 
     render: function() { 

      console.log(this.collection.toJSON()) 
      this.$el.html(template()); 
      //this.initFancyTree(); 
      return this; 
     } 
    }); 
}) 
+0

[DynamicがRequireJSで必要となり、「モジュール名がまだコンテキストにロードされていません」というエラーが発生しましたか?](http://stackoverflow.com/questions/17446844/dynamic-require-in-requirejs-getting-module-まだ名前がついていません。 –

+0

質問をする前に、ドキュメントを読んでコードをデバッグしてください。 [あなたが問題を抱えていた瞬間から15分はありませんでした](http://stackoverflow.com/questions/40712506/error-loading-jquery-ui-in-require-js-config-for-fancytree-plugin#comment68653176_40712532あなたが質問を投稿した瞬間。 –

答えて

2

RequireJS tries to emulateテンプレートをロードすることにより、非同期に。

これを使用しようとするとまだ準備ができていないため、エラーがスローされます。

あなたはそれを動作させるために次のことを唯一の必要があるでしょう:

define([ 
    'jquery', 'underscore', 'backbone', 'jquery-ui', 'fancytree', 
    'text!templates/categories.html' 
], function(
    $, _, Backbone, ui, fancytree, 
    tpl 
) { 
    'use strict'; 
    return Backbone.View.extend({ 
     el: $('#tree'), 
     template: _.template(tpl), // can be put here directly 
     initialize: function() { 
      // only has 3 parameters 
      this.listenTo(this.collection, 'reset add change remove', this.render); 
      this.collection.fetch(); 
     }, 
     render: function() { 
      this.$el.html(this.template()); 
      return this; 
     } 
    }); 

}); 

追加情報(および類似の質問):

+0

ありがとうございましたあなたの助けが大変感謝しています - 歓声 – ONYX

関連する問題

 関連する問題