【发布时间】:2014-06-11 06:51:29
【问题描述】:
我有一个 BackboneJS 模型/集合,如下所示;
var PersonInfo = BaseModel.extend({
idAttribute: "PersonInfoId",
urlRoot: "api/outline/",
defaults: {
"name": null,
"class": null
}
});
var PersonInfoCollection = BaseCollection.extend({
url: "api/outline/",
model: PersonInfo
});
现在我有一个网格,其中填充了来自后端的响应/数据(REST 调用) 所以我说;
var personInfoCollection = new PersonInfoCollection();
myGrid.backboneRegColl(personInfoCollection);
我的方法也定义为;
Grid.prototype.backboneRegColl = function(collection, parameters) {
var self = this;
collection.setServerParameters(pars);
collection.bind("dataReady", function(data) {
//Clear the grid data
self.clear();
//Get data from collection
var data = collection.toJSON();
//Render data from collection
self.renderData(data);
});
}
renderData 也定义如下;
Grid.prototype.renderData = function(data) {
var self = this;
//Empty data rows
this.el.find("tbody").empty();
//get data from grid storage
var dataToRender = this.data;
//Override data to render from arguments
if(data != undefined) {
dataToRender = data;
}
if(dataToRender.length && ! $.isArray(dataToRender)) {
dataToRender = dataToRender.toArray();
}
//If the dataToRender is an array
if($.isArray(dataToRender)) {
//If its an empty array
//XXX
if(dataToRender.length === 0) {
//Show no records message
_createEmptyRecordsMessage.call(this);
}
else {
//Render each row
$.each(dataToRender, function(index, dataRow) {
$frag.append(_createDataRow.call(self, dataRow));
});
$("tbody", this.el).append($frag);
}
}
//If the dataToRender is null, undefined or not an array
else {
//Show no records message
_createEmptyRecordsMessage.call(this);
}
//Resize is called to make sure header and data table widths are aligned
this._resize();
function _createDataRow(data) {
var tr = $("<tr>").attr("tabIndex","-1"),
self = this;
//For each column in column definitions
$(this.cols).each(function(index, column){
//Create a cell
var cell = $("<td>");
var dataValue = $("<div>").html("-");
if($.isFunction(column.renderer)) {
//Call the renderer and place the returned html|string into the data container
dataValue.html(column.renderer.call(data, $.extend(true, {}, column)));
}
else if(data[column.id] !== undefined && data[column.id] !== null) {
//Set the text of the data container to the data
dataValue.html(data[column.id]).attr('title', data[column.id]);
}
cell.append(dataValue);
tr.append(cell);
});
//store the data for the row in the html row
tr.data(data);
return tr;
}
}
现在我的问题是我没有准备好 REST API...那么如何使用本地 JSON 数据测试网格是否正确填充?
【问题讨论】:
-
为什么不简单地将所有示例数据硬编码到您的代码中
-
我需要使用静态主干模型/集合数据进行测试
标签: javascript jquery ajax rest backbone.js