Používam text.js načítať šablóny a dostanem chybu
require("text!templates/categories.html")
chyba je
Uncaught Error: Module name "text!templates/categories.html_unnormalized2" has not been loaded yet for context:
Pozrite sa na sekciu môjho pohľadu v procese vyžadujúcom načítanie šablóny, kde sa nachádza chyba.
Štruktúra adresára projektu:
js
/app
....
views/categories
templates/categories
/lib
/jquery
/underscore
/backbone
/text
/vendor
/jquery-ui
/fancytree
Požadované konfigurácie:
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",
});
vyhliadka:
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;
}
});
})
odpovede:
2 pre odpoveď č. 1Snažíte sa vyžadovať moduly pomocou syntaxe CommonJS, ktoré RequireJS sa snaží napodobňovať tým, že šablónu načítate asynchrónne.
Pretože ešte nie je pripravený pri pokuse o jeho použitie, hodí chybu.
Na to, aby ste ju dostali do práce, potrebujete nasledujúce:
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;
}
});
});
Ďalšie informácie (a podobné otázky):
- Dynamická požiadavka v aplikácii RequireJS, získanie chyby "Názov modulu nebol naložený za kontext"?
- Názov modulu ešte nebol načítaný pre kontext
- Chyba: Názov modulu "xxx" zatiaľ nebol načítaný pre kontext