我想使用 Require.js 文本为 Backbone 视图加载外部 HTML 模板!插件并自定义模板路径(从查看“模板”属性获取模板路径)。问题是 - 模板异步加载并且在渲染时我仍然没有它。这是我的代码:
自定义视图
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;
});
它有效,但我不喜欢听“模板:加载”事件进行渲染。有什么建议么?谢谢!