Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将参数传递给集合解析?backbone.js_Javascript_Backbone.js - Fatal编程技术网

Javascript 如何将参数传递给集合解析?backbone.js

Javascript 如何将参数传递给集合解析?backbone.js,javascript,backbone.js,Javascript,Backbone.js,如何通过parse/fetch函数传递参数? 我想传递下初始化部分中的变量变量\u参数 否则我就得写三本几乎一模一样的书 谢谢你的帮助 app.js //-------------- // Collections //-------------- DiagnoseApp.Collections.Param1_itemS = Backbone.Collection.extend({ model: DiagnoseApp.Models.Param1_item,

如何通过parse/fetch函数传递参数? 我想传递下初始化部分中的变量变量\u参数

否则我就得写三本几乎一模一样的书

谢谢你的帮助

app.js

//--------------
// Collections
//--------------

    DiagnoseApp.Collections.Param1_itemS = Backbone.Collection.extend({
        model: DiagnoseApp.Models.Param1_item,
        url: 'TestInterface.xml',
        parse: function (data) { 
            var parsed = [];

            $(data).find(/*VARIABLE_PARAMETER*/).find('PARAMETER').each(function (index) {
                var v_number = $(this).attr('Number');
                var v_Desc_D = $(this).attr('Desc_D');
                parsed.push({ data_type: v_data_type, number: v_number, Desc_D: v_Desc_D});
            });

            return parsed;
        },

        fetch: function (options) { 
            options = options || {};
            options.dataType = "xml";
            return Backbone.Collection.prototype.fetch.call(this, options);
        }
    });
这是我初始化应用程序的方式:

    //--------------
    // Initialize
    //--------------

    var VARIABLE_PARAMETER = "OFFLINE";

    var offline_Collection = new DiagnoseApp.Collections.Param1_itemS();
    var offline_Collection_View = new DiagnoseApp.Views.Param1_itemS({collection: offline_Collection});


    //VARIABLE_PARAMETER has to be passed here in fetch I guess ??

     offline_Collection.fetch({
        success: function() {
              console.log("JSON file load was successful", offline_Collection);

              offline_Collection_View.render();
          },
        error: function(){
           console.log('There was some error in loading and processing the JSON file');
        }

    });

我不确定我是否理解您的问题,但是如果您想将参数从
fetch
传递到
parse
,并且如果给定集合的参数值没有更改,您可以将其存储在集合中。您可以将参数作为
选项
中的附加属性传递给
获取

fetch: function (options) { 
    options = options || {};
    options.dataType = "xml";
    this.variableParameter = options.variableParameter;
    return Backbone.Collection.prototype.fetch.call(this, options);
},
然后简单地检索它

parse: function (data) { 
    // do something useful with this.variableParameter
    // ...
}

fetch
方法接受一个
选项
参数:
parse
方法还接受一个
选项
参数: 这些对象实际上是相同的。所以你可以写:

parse: function (data, options) { 
        var parsed = [];

        $(data).find(options.variableParameter).find('PARAMETER').each(function (index) {
            var v_number = $(this).attr('Number');
            var v_Desc_D = $(this).attr('Desc_D');
            parsed.push({ data_type: v_data_type, number: v_number, Desc_D: v_Desc_D});
        });

        return parsed;
    },

为什么不在
XML
中有一个单独的节点,当您从BE发送响应时,它是您的变量参数呢?
XML文件
是固定的。所以可以这样调用它:
offlineCollection.fetch({success:{..},error:{..},variable\u参数)?否,
fetch
方法只接受一个参数:
offlineCollection.fetch({success:{..},error:{..},variable_参数:variable_参数})