为什么我的主干视图渲染将“未定义”置于在顶部?
我认为以下视图中的大部分渲染函数都是无关紧要的,但为了完整性我将其保留。我正在逐步完成工作流程步骤列表,并注意到根据事物的不同状态切换 li
的类。
var WorkStepView = Backbone.View.extend({
className: "workflowStep",
render: function() {
var output;
var arrowPlaced = false;
var self = this;
i = 0;
_.each(this.collection.models, function(s) {
var post = "";
var classname = '';
if (s.attributes["complete"] != 1) {
if (! arrowPlaced) {
classname = "current";
if (s.attributes["adminCompletable"] == 1 ) {
post = ' - <input onClick="completeStep(' + i + ');" type="button" value="Completed" />';
}
arrowPlaced = true;
}
else {
classname = "future";
}
}
else {
classname = "past";
}
output += '<li class="' + classname + '">' + s.attributes["description"] + post + '</li>';
i++;
});
this.el.innerHTML = output;
return this;
},
});
当用户从页面其他位置的列表中选择资产时,它会显示一堆包含这些内容的数据。资产“有”一个工作流程,其中“有”许多步骤。步骤数据存储在 window.Steps 中,然后调用它:
function populateSteps() {
window.workStepLists = new WorkStepView({
collection: window.Steps,
el: $('#workflowSteps')[0],
});
workStepLists.render();
}
但是然后我得到这个:
列表顶部有一个无关的“未定义”。在获取和渲染此模型之前它不存在。我根本不知道这是怎么回事。
帮助?
Most of the render function in the following view is irrelevant, I think but I'm leaving it for completeness. I'm stepping through a list of workflow steps, noting switching the class of the li
depending on various states of the thing.
var WorkStepView = Backbone.View.extend({
className: "workflowStep",
render: function() {
var output;
var arrowPlaced = false;
var self = this;
i = 0;
_.each(this.collection.models, function(s) {
var post = "";
var classname = '';
if (s.attributes["complete"] != 1) {
if (! arrowPlaced) {
classname = "current";
if (s.attributes["adminCompletable"] == 1 ) {
post = ' - <input onClick="completeStep(' + i + ');" type="button" value="Completed" />';
}
arrowPlaced = true;
}
else {
classname = "future";
}
}
else {
classname = "past";
}
output += '<li class="' + classname + '">' + s.attributes["description"] + post + '</li>';
i++;
});
this.el.innerHTML = output;
return this;
},
});
When the user selects an asset from a list elsewhere on the page, it brings up a bunch of data including this stuff. An asset "has" a workflow, which "has" many steps. The step data is stored in window.Steps, and then this is called:
function populateSteps() {
window.workStepLists = new WorkStepView({
collection: window.Steps,
el: $('#workflowSteps')[0],
});
workStepLists.render();
}
But then I get this:
There's this extraneous "undefined" up there at the top of the list. It wasn't there before fetching and rendering this model. I don't know what's up with that at all.
Help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有将
output
初始化为任何内容。因此,output += "foo"
变为“undefinedfoo”。只需使用空字符串初始化输出变量:
You are not initializing
output
to be anything. So,output += "foo"
becomes "undefinedfoo".Simply initialize your output variable with an empty string: