javascript - How to pass a parameter to Collection parse? backbone.js -
how 1 pass parameter through parse/fetch function? want pass variable variable_parameter in lower initialize-part.
otherwise have write 3 identical collections.
thank you help.
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); } });
this way initialize app:
//-------------- // 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 passed here in fetch guess ?? offline_collection.fetch({ success: function() { console.log("json file load successful", offline_collection); offline_collection_view.render(); }, error: function(){ console.log('there error in loading , processing json file'); } });
the fetch
method accepts option
argument : http://backbonejs.org/#collection-fetch parse
method accepts option
argument: http://backbonejs.org/#collection-parse these objects same. may write:
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; },
Comments
Post a Comment