我有一个主干视图,它的渲染函数中有以下代码(注意 3 console.log(that.ticketSelector.attributes);
):
if(typeof this.ticketSelector === 'undefined') {
// TODO: first fetch tickets
this.ticketSelector = new DataTable({
collection: that.model.tickets,
icon: "ticket",
title: "Tickets",
customClasses: ["ticket", "subject-selector"],
columns: [
{text: 'Name', modelKey: "name", col: 1},
{text: 'Date', modelKey: "date", col: 2},
{text: 'Owner', modelKey: "owner", col: 3},
{text: 'Description', modelKey: "description", col: 4}
]
});
this.$('.subject-selectors').append(this.ticketSelector.$el);
this.ticketSelector.render().resize();
} else {
this.ticketSelector.render().resize();
}
console.log(that.ticketSelector.attributes);
if(typeof this.alarmSelector === 'undefined') {
// TODO: first fetch tickets
this.alarmSelector = new DataTable({
collection: that.model.alarms,
icon: "warning-sign",
title: "Alarms",
customClasses: ["alarm", "subject-selector"],
columns: [
{text: 'Name', modelKey: "name", col: 1},
{text: 'Date', modelKey: "date", col: 2},
{text: 'Owner', modelKey: "owner", col: 3},
{text: 'Description', modelKey: "description", col: 4}
]
});
this.$('.subject-selectors').append(this.alarmSelector.$el);
this.alarmSelector.render().resize();
} else {
this.alarmSelector.render().resize();
}
console.log(that.ticketSelector.attributes);
if(typeof this.taskSelector === 'undefined') {
// TODO: first fetch tickets
this.taskSelector = new DataTable({
collection: that.model.tasks,
icon: "tasks",
title: "Tasks",
customClasses: ["task", "subject-selector"],
columns: [
{text: 'Name', modelKey: "name", col: 1},
{text: 'Date', modelKey: "date", col: 2},
{text: 'Owner', modelKey: "owner", col: 3},
{text: 'Description', modelKey: "description", col: 4}
]
});
this.$('.subject-selectors').append(this.taskSelector.$el);
this.taskSelector.render().resize();
} else {
this.taskSelector.render().resize();
}
console.log(that.ticketSelector.attributes);
在控制台中我看到:
Object {icon: "ticket", title: "Tickets", columns: Array[4], classes: Array[2]}
Object {icon: "warning-sign", title: "Alarms", columns: Array[4], classes: Array[2]}
Object {icon: "tasks", title: "Tasks", columns: Array[4], classes: Array[2]}
我期望的地方:
Object {icon: "ticket", title: "Tickets", columns: Array[4], classes: Array[2]}
Object {icon: "ticket", title: "Tickets", columns: Array[4], classes: Array[2]}
Object {icon: "ticket", title: "Tickets", columns: Array[4], classes: Array[2]}
这是我的数据表视图:
var DataTable = Backbone.View.extend({
tag: 'div',
className: 'data-table',
initialize: function(opts) {
if(typeof opts.parent !== 'undefined') {
this.parent = opts.parent;
}
if(typeof opts.icon !== 'undefined') {
this.attributes.icon = opts.icon;
}
if(typeof opts.title !== 'undefined') {
this.attributes.title = opts.title;
}
if(typeof opts.columns !== 'undefined') {
this.attributes.columns = opts.columns;
}
if(typeof opts.customClasses !== 'undefined') {
this.attributes.classes = opts.customClasses;
}
},
attributes: {},
template: function() {
var temp;
$.ajax(root + 'templates/data-table.html', {
success: function(data) {
// console.log(data);
temp = Mustache.compile($(data).filter('#data-table-template').html());
},
async: false
});
return temp;
}(),
events: {
},
serialize: function() {
var that = this;
return {
root: root,
icon: that.attributes.icon,
title: that.attributes.title,
columns: that.attributes.columns
};
},
resize: function() {
var that = this;
},
subView: [],
render: function() {
var that = this;
var html = this.template(this.serialize());
this.$el.html(html);
if(that.attributes.classes) {
_.each(that.attributes.classes, function(c) {
that.$el.addClass(c);
});
}
this.collection.each(function(row) {
tempView = new DataTableRow({ model: row, parent: that, columns: that.attributes.columns });
that.subView.push(tempView);
that.$('.tbody').append(tempView.$el);
tempView.render();
});
this.$('.tbody').mCustomScrollbar({
scrollInertia: 0,
});
return this;
}
});
var DataTableRow = Backbone.View.extend({
tag: 'div',
className: 'tr',
initialize: function(opts) {
var that = this;
if(typeof opts.parent !== 'undefined') {
this.parent = opts.parent;
}
if(typeof opts.columns !== 'undefined') {
var temp = {};
that.attributes.columns = _.map(opts.columns, function(col) {
return {
text: that.model.get(col.modelKey),
col: col.col
};
});
}
},
attributes: { columns: [] },
template: function() {
var temp;
$.ajax(root + 'templates/data-table.html', {
success: function(data) {
// console.log(data);
temp = Mustache.compile($(data).filter('#data-table-row-template').html());
},
async: false
});
return temp;
}(),
events: {
},
serialize: function() {
var that = this;
return {
root: root,
columns: that.attributes.columns
};
},
resize: function() {
var that = this;
},
render: function() {
var that = this;
var html = this.template(this.serialize());
this.$el.html(html);
return this;
}
});
我知道我可以通过为每个数据表创建不同的视图来解决它,但这应该可行,我不知道为什么它不可行。这里有什么帮助吗?谢谢。
编辑:为了更好地理解这一点,这里是下划线扩展功能:
_.extend = function(obj) {
each(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop];
}
}
});
return obj;
};
是什么让这些属性附加到原型上?