I have a unique text file containing date-time data in the format below:
2014-03-14T16:32
2014-03-15T13:04
2014-03-16T06:44
...
I want to use this static file as a read-only data source for my backbone collection. However, the current format is not suitable for direct consumption. My initial approach was to utilize the parse method in my collection to transform it into an array of objects. Unfortunately, this strategy has faced some obstacles.
Firstly, without overriding fetch, the parse method does not get invoked properly and triggers an error handler instead. The reason behind this behavior is unclear, as no actual errors are being thrown. I suspect that it may be due to expecting a different response type.
Secondly, even when both fetch and parse methods are overridden as shown below:
var MyColl = Backbone.Collection.extend({
model: MyModel,
url: 'date-list.txt',
parse: function(data) {
var result = _(data.split("\n")).compact().map(function(item, i) {
return { theDate: item, globalIndex: i };
});
return result;
},
fetch: function() {
$.get(this.url, this.parse);
}
});
Although the parse function correctly processes the data and constructs valid objects, the resulting collection remains empty after completion. It appears that invoking parse outside the expected flow leads to the data not being utilized. Are there any alternative strategies to ensure fetch handles the server response appropriately?
While options like having the server provide JSON or implementing a custom fetching function externally exist, I am exploring possibilities within the bounds of backbone itself before resorting to those solutions.