Templates Backbone.js-使用Require.js文本插件预加载外部模板
我想加载带有Require.js文本的主干视图的外部HTML模板!插件并自定义模板路径(从视图“模板”属性获取模板路径)。问题是-模板异步加载,在渲染时我仍然没有它。这是我的密码: 自定义视图Templates Backbone.js-使用Require.js文本插件预加载外部模板,templates,backbone.js,requirejs,Templates,Backbone.js,Requirejs,我想加载带有Require.js文本的主干视图的外部HTML模板!插件并自定义模板路径(从视图“模板”属性获取模板路径)。问题是-模板异步加载,在渲染时我仍然没有它。这是我的密码: 自定义视图 define([ 'views/abstract/base-view', ], function (BaseView) { var AppView = BaseView.extend({ el: '#app-view', template: 'app-
define([
'views/abstract/base-view',
], function (BaseView) {
var AppView = BaseView.extend({
el: '#app-view',
template: 'app-view',
initialize: function () {
this.on('template:loaded', this.render, this);
BaseView.prototype.initialize.apply(this, arguments);
},
render: function () {
this.$el.empty();
var html = this.template();
this.$el.html(html);
}
});
return AppView;
});
继承和抽象视图
define([], function () {
var AbstractView = Backbone.View.extend({
initialize: function(){
this.setTemplate();
},
setTemplate: function(){
var that = this;
require(['3p/text!templates/' + this.template + '.tpl'], function(tpl) {
that.template = _.template(tpl);
that.trigger('template:loaded');
});
}
});
return AbstractView;
});
它可以工作,但我不喜欢为渲染监听“template:loaded”事件。有什么建议吗?
谢谢 我也有同样的问题。
为了避免异步行为,除了添加一些需要的动态文件并在加载之前加载所有文件之外,没有其他方法
我做了类似于注射的东西。我在一个文件中加载了所有的动态模板,并将模板的路径放在一个共享字典中。然后我使用u.template(require(inject[“templateNAME”]),此时我得到了模板
这个想法类似于下一个:
define(["require"], function (require) {
require(["text!path/of/the/template",
... ]);
window.inject = {};
inject.templateNAME = "text!path/of/the/template";
然后,您将使用以下内容:
var tpl = require(inject[this.templateName]);
this.template = _.template(tpl);