Backbone.js 主干渲染返回在视图中未定义
我正在学习主干,我似乎对下面的渲染方法有问题。运行渲染时,渲染始终返回未定义。请任何人帮帮忙Backbone.js 主干渲染返回在视图中未定义,backbone.js,Backbone.js,我正在学习主干,我似乎对下面的渲染方法有问题。运行渲染时,渲染始终返回未定义。请任何人帮帮忙 var Todo = Backbone.Model.extend({ defaults : { status: 'true' , description : 'Nothing' }, toggleStatus: function() {
var Todo = Backbone.Model.extend({
defaults : {
status: 'true' ,
description : 'Nothing'
},
toggleStatus: function() {
if (this.get('status') === "completed")
this.set({
'status': 'completed'
});
else
this.set({
'status': 'incompleted'
});
}
});
// Simple View
var simpleView = Backbone.View.extend({
events: {
'change input': 'toggleStatus'
},
initialize: function() {
this.model.on('change', this.render, this);
},
toggleStatus: function() {
this.model.toggleStatus();
},
template : _.template('<h3 class="<%= status %>">' +
'<% if(status === "complete") print("checked") %>/>' +
' <%= description %></h3>'),
render: function() {
console.log('render was called');
$(this.el).html(this.template(this.model.toJSON()));
// return this;
}
});
var todonew = new Todo();
var newView = new simpleView({
model: todonew
});
var Todo=Backbone.Model.extend({
默认值:{
状态:“true”,
描述:“没有”
},
toggleStatus:function(){
if(this.get('status')=“completed”)
这台({
“状态”:“已完成”
});
其他的
这台({
“状态”:“未完成”
});
}
});
//简单视图
var simpleView=Backbone.View.extend({
活动:{
“更改输入”:“切换状态”
},
初始化:函数(){
this.model.on('change',this.render,this);
},
toggleStatus:function(){
this.model.toggleStatus();
},
模板:u.template(“”+
'/>' +
' '),
render:function(){
log('调用了render');
$(this.el).html(this.template(this.model.toJSON());
//归还这个;
}
});
var todonew=新Todo();
var newView=newsimpleview({
型号:todonew
});
您可能应该在模板的if
语句中放上括号,这可能会在某些浏览器中造成问题。改变这个
'<% if(status === "complete") print("checked") %>/>' +
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
选项2:在创建视图时,发送要向其中添加内容的元素
var newView = new simpleView({
model: todonew,
el: $('body') //or whatever selector you want
});
选项3:在渲染函数中更改此行
$(this.el).html(this.template(this.model.toJSON()));
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
同样,这将替换整个页面的HTML,根据需要使用不同的选择器,或使用
.append()
或其他方法代替.HTML()
您可能应该在模板的if
语句中加括号,这可能会在某些浏览器中造成问题。改变这个
'<% if(status === "complete") print("checked") %>/>' +
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
选项2:在创建视图时,发送要向其中添加内容的元素
var newView = new simpleView({
model: todonew,
el: $('body') //or whatever selector you want
});
选项3:在渲染函数中更改此行
$(this.el).html(this.template(this.model.toJSON()));
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
同样,这将替换整个页面的HTML,根据需要使用不同的选择器,或使用
.append()
或其他方法代替.HTML()
您可能应该在模板的if
语句中加括号,这可能会在某些浏览器中造成问题。改变这个
'<% if(status === "complete") print("checked") %>/>' +
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
选项2:在创建视图时,发送要向其中添加内容的元素
var newView = new simpleView({
model: todonew,
el: $('body') //or whatever selector you want
});
选项3:在渲染函数中更改此行
$(this.el).html(this.template(this.model.toJSON()));
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
同样,这将替换整个页面的HTML,根据需要使用不同的选择器,或使用
.append()
或其他方法代替.HTML()
您可能应该在模板的if
语句中加括号,这可能会在某些浏览器中造成问题。改变这个
'<% if(status === "complete") print("checked") %>/>' +
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
选项2:在创建视图时,发送要向其中添加内容的元素
var newView = new simpleView({
model: todonew,
el: $('body') //or whatever selector you want
});
选项3:在渲染函数中更改此行
$(this.el).html(this.template(this.model.toJSON()));
对此
'<% if(status === "complete") {print("checked")} %>/>' +
$('body').html(this.template(this.model.toJSON()));
$('body').html(this.template(this.model.toJSON()));
同样,这将替换整个页面的HTML,根据需要使用不同的选择器,或使用
.append()
或其他方法代替.HTML()
您需要设置el
,这是脚本在this
上返回错误时出现的问题请尝试以下示例:
var newView = new simpleView({
model: todonew,
el: $("#somediv") //put here a div or the element where you want to put your template
});
您需要设置
el
,这是脚本在this
上返回错误时出现的问题。请尝试以下示例:
var newView = new simpleView({
model: todonew,
el: $("#somediv") //put here a div or the element where you want to put your template
});
您需要设置
el
,这是脚本在this
上返回错误时出现的问题。请尝试以下示例:
var newView = new simpleView({
model: todonew,
el: $("#somediv") //put here a div or the element where you want to put your template
});
您需要设置
el
,这是脚本在this
上返回错误时出现的问题。请尝试以下示例:
var newView = new simpleView({
model: todonew,
el: $("#somediv") //put here a div or the element where you want to put your template
});
我在写作时也有类似的问题:
$('#foo').append(bar.render().el);
我得到了这个错误:
Uncaught TypeError: Cannot read property 'el' of undefined
为了纠正这个问题,我添加了返回这个代码>在我的呈现:函数(){}
中
render: function() {
// something else
return this; // add this line
}
参考:
一个好的约定是在渲染结束时返回该值以启用链接调用
我在写作时也有类似的问题:
$('#foo').append(bar.render().el);
我得到了这个错误:
Uncaught TypeError: Cannot read property 'el' of undefined
为了纠正这个问题,我添加了返回这个代码>在我的呈现:函数(){}
中
render: function() {
// something else
return this; // add this line
}
参考:
一个好的约定是在渲染结束时返回该值以启用链接调用
我在写作时也有类似的问题:
$('#foo').append(bar.render().el);
我得到了这个错误:
Uncaught TypeError: Cannot read property 'el' of undefined
为了纠正这个问题,我添加了返回这个代码>在我的呈现:函数(){}
中
render: function() {
// something else
return this; // add this line
}
参考:
一个好的约定是在渲染结束时返回该值以启用链接调用
我在写作时也有类似的问题:
$('#foo').append(bar.render().el);
我得到了这个错误:
Uncaught TypeError: Cannot read property 'el' of undefined
为了纠正这个问题,我添加了返回这个代码>在我的呈现:函数(){}
中
render: function() {
// something else
return this; // add this line
}
参考:
一个好的约定是在渲染结束时返回该值以启用链接调用
render不必返回任何东西,它是一个打印结果的函数,真正的问题是什么?我在chrome开发工具中运行了newView.render(),但结果没有定义。我希望元素将结果附加到屏幕上
被注释掉了吗?我尝试返回此内容,但收到了未捕获的SyntaxError:来自浏览器的意外标记,因此我发表了评论。您尚未指定“el”属性,因此主干将创建一个分离的并在其中呈现您的视图。这就是为什么它不会出现在屏幕上。render不必返回任何东西,它是一个打印结果的函数,真正的问题是什么?我运行了newVie