Backbone.js 将数据传递到下划线以使呈现不起作用
我正在尝试呈现下面的下划线模板Backbone.js 将数据传递到下划线以使呈现不起作用,backbone.js,underscore.js,underscore.js-templating,Backbone.js,Underscore.js,Underscore.js Templating,我正在尝试呈现下面的下划线模板 div.col-md-12#english select(value="", class="form-control") option | Select Your Language Preference script(type="text/template" id="english-pref") <% if (selec
div.col-md-12#english
select(value="", class="form-control")
option
| Select Your Language Preference
script(type="text/template" id="english-pref")
<% if (selected === "US") { %>
option(value="US", selected) | United States
<% } else %>
<% if(selected === "US"){ %>
option(value="UK", selected) | United Kingdom
<% } %>
div.col-md-12#英语
选择(value=”“,class=“表单控制”)
选项
|选择您的语言偏好
脚本(type=“text/template”id=“english pref”)
选项(value=“US”,选中)|美国
选项(value=“UK”,选中)|英国
这是我的主干视图代码
app.NotesView = Backbone.View.extend({
el: '#notes',
events: {
'click #save': 'save',
'click #update': 'update'
},
template1: _.template($('#about').html()),
template2: _.template($('#facts').html()),
template3: _.template($('#english').html()),
initialize: function() {
app.NotesModel = new app.NotesModel({});
this.model = app.NotesModel;
app.email = $('#data-user').text();
app.username = $('#data-username').text();
app.NotesModel.fetch({
data: {
email: app.email,
username: app.username
},
type: 'POST',
processData: true,
success: function(data) {
$('#save').remove();
},
complete: function(xhr, textStatus) {
//console.log(textStatus);
},
error: function() {
$('#about-container .note-editable').html('');
$('#note-container .note-editable').html('');
$('#update').remove();
}
});
this.model.bind('sync', this.render, this);
},
render: function() {
this.$('#aboutParent').html(this.template1, this);
this.$('#noteParent').html(this.template2, this);
app.initializeEditor();
$('#about-container .note-editable').html(this.model.attributes.about);
$('#note-container .note-editable').html(this.model.attributes.editorNote);
if(this.model.attributes.english === null || this.model.attributes.english === undefined || this.model.attributes.english === '') {
/*var data = '<option value="US">United States</option><option value="UK">United Kingdom</option>';*/
var data = {"selected": "US"};
this.$('#noteParent').html(this.template3,data);
}else {
var data = {"selected": "UK"};
this.$('#noteParent').html(this.template3,data);
}
}
});
app.NotesView=Backbone.View.extend({
el:'注释',
活动:{
'单击#保存':'保存',
'单击#更新':'更新'
},
template1:3;.template($('#about').html()),
template2:3;.template($('#facts').html()),
template3:35;.template($('#english').html()),
初始化:函数(){
app.NotesModel=新建app.NotesModel({});
this.model=app.NotesModel;
app.email=$(“#数据用户”).text();
app.username=$('#数据用户名').text();
app.NotesModel.fetch({
数据:{
电子邮件:app.email,
用户名:app.username
},
键入:“POST”,
processData:对,
成功:函数(数据){
$(“#保存”).remove();
},
完成:功能(xhr、textStatus){
//console.log(textStatus);
},
错误:函数(){
$('#关于container.note可编辑').html('';
$('#note container.note editable').html('';
$(“#更新”).remove();
}
});
this.model.bind('sync',this.render,this);
},
render:function(){
this.$('#aboutParent').html(this.template1,this);
this.$('#noteParent').html(this.template2,this);
app.initializeditor();
$('#about container.note editable').html(this.model.attributes.about);
$('#note container.note editable').html(this.model.attributes.editorNote);
if(this.model.attributes.english==null | | | this.model.attributes.english===undefined | | | this.model.attributes.english===“”){
/*var数据='美国独立王国';*/
变量数据={“选定”:“US”};
this.$('#noteParent').html(this.template3,data);
}否则{
变量数据={“选定”:“UK”};
this.$('#noteParent').html(this.template3,data);
}
}
});
我看了几个教程,我真的很困惑,因为每个教程都有自己的完成方式。我现在面临的问题是,我的模板没有呈现,因为它说“选定”是未定义的。我是否将对象正确地传递给视图
还有一个模式,我可以用它作为呈现模板的经验法则。
这个。template3
(和template1
和template2
)是一个函数,您可以使用数据作为参数调用它来获取HTML。一般而言:
var tmpl = _.template(template_source_text);
var html = tmpl(template_data);
您只是将模板函数传递给jQuery的html
:
this.$('#noteParent').html(this.template3,data);
传递函数时,它会调用该函数,但不会使用模板函数所需的参数:
.html(函数)类型:函数(整数索引,htmlString OLDHML)=>htmlString 返回要设置的HTML内容的函数。接收集合中元素的索引位置和旧HTML值作为参数 你所做的和说的一样:
this.$('#noteParent').html(this.template3(some_index, some_html));
你想说:
this.$('#noteParent').html(this.template3(data));
因此,
data
中的内容被传递到模板函数。谢谢,我做了一些尝试,发现了问题,部分原因是我的下划线,部分原因是与jade冲突。