2

I have the following Backbone Model and Collection

/**
 *  DataPoint
 */
var DataPoint = Backbone.Model.extend({
    defaults: {
        ts: null,
        value: null
    }
});

var DataPointCollection = Backbone.Collection.extend({
    model: DataPoint
});

In order to populate and do what I need to do with the data I do something similar to this:

url = '/api/v1/database/1/data';
$.getJSON(url, params, function(data) {

   var dps = new DataPointCollection;
   dps.add(data.datapoints);
   //Now do stuff with dps

});   

I'm sure there is a better way to do this with Backbone but not sure how. I feel it should be more like telling the DataPoint collection to populate itself. How to approach this on backbone?

leonsas
  • 4,718
  • 6
  • 43
  • 70

1 Answers1

3

Have a look at the docs, fetch is what you're looking for; here's the example took from there:

var accounts = new Backbone.Collection;
accounts.url = '/accounts';
accounts.fetch();
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106