0

我有一个问题,我的模型中的集合属性(在本例中为参数集合)未包含在 JSON.stringify 函数创建的 JSON 字符串中。有什么理由可能会发生这种情况吗?它基本上只是将其排除在外,并将其余变量添加到 JSON 字符串中。

这是事件:

                EventAggregator.on('toggleFacet', function (facets) {
                var facets = SearchOptionsUtil.getCheckedFacets(facets);
                var sortOptions = SearchOptionsUtil.getSortOptions();

                var searchOptions = new SearchOptionsModel();

                for(var facet in facets){
                    var id = facet;
                    var value = facets[facet];
                    searchOptions.parameters.add(new ParameterModel({id: id, values: value.split(',')}));
                }

                var criteria = $.extend(facets, sortOptions);
                location.hash = UriUtil.getUriHash(criteria);

                RequestUtil.requestSearchResults(searchOptions);
            });

这是获取:

requestSearchResults: function (searchOptions) {
            //fetch the results
            var performSearchModel = new PerformSearchModel();
            var searchOptionsJson = JSON.stringify(searchOptions);
            performSearchModel.fetch({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({searchOptionsJson: searchOptionsJson}),
                success: function (response) {
                    console.log("Inside success");
                    console.log(response);
                },
                error: function (errorResponse) {
                    console.log("Inside Failure")
                    console.log(errorResponse.responseText)
                }
            })  //have to wait for the fetch to complete
                .complete(function () {

                    //show our regions
                    App.facetsRegion.show(new FacetListView({collection: performSearchModel.facets}));
                    App.resultsRegion.show(new ResultListView({collection: performSearchModel.results}));

                    //perform search fetch complete
                });
        }

这是模型:

var SearchOptionsModel = Backbone.Model.extend({
        defaults: {
            parameters: ParameterCollection,
            currentItemId: '{EE8AA76E-0A3E-437B-84D8-AD7FCBAF2928}',
            sortBy: 0,
            sortDirection: 'asc',
            resultsPerPage: 10
        },
        initialize: function () {
            this.parameters = new ParameterCollection();

            //fetch calls an on change event.
            this.on("change", this.fetchCollections);
        },
        url: function () {
            return '/Services/Search/SearchService.asmx/SearchOptions';
        },
        parse: function (response) {
            var data = JSON.parse(response.d);
            return data;
        },
        fetchCollections: function () {
            //when we call fetch for the model we want to fill its collections
            this.parameters.set(
                _(this.get("parameters")).map(function (parameter) {
                    return new ParameterModel(parameter);
                })
            );
        }
    });

更新**

所以我改变了在 SearchOptionsModel 中创建和添加参数集合的方式,并且 JSON 对象正在正确形成。我从这里改变了它:

 var searchOptions = new SearchOptionsModel();

            for(var facet in facets){
                var id = facet;
                var value = facets[facet];
                searchOptions.parameters.add(new ParameterModel({id: id, values: value.split(',')}));
            }

对此:

                 var parameters = new ParameterCollection();

                //loop through all of the variables in this object
                for(var facet in facets){
                    var id = facet;
                    var value = facets[facet];

                    parameters.add(new ParameterModel({id: id, values: value.split(',')}));
                }

                var searchOptions = new SearchOptionsModel({parameters: parameters});

现在参数填充在模型中的属性下,我在 searchOptions 对象上看到了一个空的参数变量(之前是填充的)。如果我没有明确创建,为什么 SearchOptionsModel 中设置了参数变量?是因为参数默认设置为集合吗?

4

1 回答 1

0

要将 Backbone 模型转换为 JSON,您必须使用以下toJSON方法:

model.toJSON();

在此处查看文档:http: //backbonejs.org/#Model-toJSON

于 2013-09-20T20:05:26.703 回答