Как использовать несколько шаблонов JST в одном виде магистрали
Почему этот код приводит к следующей ошибке в функции рендеринга?
Uncaught TypeError: Property 'template' of object [object Object] is not a function - Line 21
KAC.Views.ScreenImportGoogle = Backbone.View.extend({
tagName: "div",
id: "",
className: "",
template1: JST['screens/import/google/unauthenticated'],
template2: JST['screens/import/google/authenticated'],
template3: JST['screens/import/google/imported'],
initialize: function() {
if (this.options.user.google_auth == false) { this.template = this.options.template1 }
else if (this.options.user.google_import == false) { this.template = this.options.template2 }
else if (this.options.user.google_import == true ) { this.template = this.options.template3 };
$('#screen-container').html(this.render().$el);
},
events: {
},
render: function () {
this.$el.html(this.template({ user: this.options.user }))
return this;
}
});
1 ответ
Вы можете попробовать сделать что-то вроде этого:
KAC.Views.ScreenImportGoogle = Backbone.View.extend({
tagName: "div",
id: "",
className: "",
template: function(){
if (this.options.user.google_auth == false) {return JST['screens/import/google/unauthenticated']}
else if (this.options.user.google_import == false) { return JST['screens/import/google/authenticated']}
else if (this.options.user.google_import == true ) { return JST['screens/import/google/imported'] };
}
});
Но вместо того, чтобы делать это, я создал бы другие представления для этих случаев авторизации Google и отобразил бы представления вместо изменения шаблона в одном представлении.