I am having difficulty in creating a client-side Backbone model that upon initialization retrieves some XML from a ExpressJS server. When the Backbone model is created, an initialization function calls model.fetch() and passes in its model ID in the query string. The server should read the ID and return the correct XML.
This currently isn't working (code below), when I debug in my browser, I can see the network request appears to be correct, for example the request could be /biomodels?id=BIOMD0000000001, but the request stays pending when it is initiated from Backbone.
However, If I use my browser (Chrome), and go to /biomodels?id=BIOMD0000000001 then the model correctly is retrieved.
Express server route handler:
/*global exports require*/
exports.getModel = function (req, res) {
// Dependencies
var biomodels = require('biomodels').BioModelsWSClient;
// Get SBML From ID
console.log('requested ID: ' + req.query.id)
console.log(biomodels.getModelSBMLById(req.query.id, function (err, results) {
console.log(err + results);
res.send(err + results);
}));
};
Backbone Model:
define([
'underscore',
'backbone'
], function (_, Backbone) {
'use strict';
var BioModel = Backbone.Model.extend({
defaults: {
sbml: 'was not fetched',
id: 'was not assigned'
},
initialize: function () {
var modelId = this.id,
modelSbml = this.sbml;
this.fetch({
data: {
id: modelId
},
type: 'GET',
error: function (jqXHR, textStatus, errorThrown) {
console.log('error in GET: ' + textStatus + errorThrown);
},
url: 'biomodels',
success: function (data, textStatus, jqXHR) {
modelSbml = data;
console.log('loaded ' + modelSbml);
}
});
}
});
return BioModel;
});