使用本地数据测试BackboneJS REST API

时间:2014-06-11 06:51:29

标签: javascript jquery ajax rest backbone.js

我有一个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数据测试网格是否正确填充?

1 个答案:

答案 0 :(得分:0)

我建议您尝试使用reset进行虚拟数据测试。

重置功能可以帮助您用新的集合替换集合并触发重置&#39;事件。我们可以用它来分配虚拟数据进行测试。

尝试注册&#39;重置&#39;集合obj上的事件监听器:

Grid.prototype.backboneRegColl = function(collection, parameters) {
    var self = this;
    collection.setServerParameters(pars);

    collection.bind("reset dataReady", function(data) {
      //Clear the grid data
      self.clear();
      //Get data from collection
      var data = collection.toJSON();
      //Render data from collection
      self.renderData(data);
    });
}

然后通过调用reset函数将伪数据分配给集合。 (请确保数据结构正确)

personalInfoCollection.reset([{id:1,name:"Jimmy",tel:"123456"},{id:2,name:"Deemon",tel:"222222"}]);

在重置&#39;之后,视图将正确呈现。事件正在被触发。