gpt4 book ai didi

javascript - 如何将参数传递给集合解析?主干.js

转载 作者:行者123 更新时间:2023-11-28 00:03:30 25 4
gpt4 key购买 nike

如何通过解析/获取函数传递参数?我想在下部初始化部分传递变量VARIABLE_PARAMETER

否则我必须编写三个几乎相同的集合。

谢谢你的帮助。

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 方法接受 option 参数:http://backbonejs.org/#Collection-fetchparse 方法还接受 option 参数:http://backbonejs.org/#Collection-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;
},

关于javascript - 如何将参数传递给集合解析?主干.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com