i'm writing a backbone.js 'app', and would like to map the json output of graphite/carbon directly to some backbone models/collections.
in general, the json output is something like this:
[
{
'target': 'some.string.1',
'datapoints': [ [ val1, timestamp1 ], [ val2, timestamp2 ]... ]
},
{
'target': 'some.string.2',
'datapoints': [ [ val1, timestamp1 ], [ val2, timestamp2 ]... ]
},
...
]
i have defined a simple model and collection such that:
class Measurement extends Backbone.Model
defaults:
id: undefined
val: undefined
class Measurements extends Backbone.Collection
model: Measurement
initialize: (model, options) ->
if options.metric
@metric = options.metric
url: ->
'/?target=' + @metric
parse: (data,xhr) ->
if _.size(data) == 1
return _(data[0]['datapoints']).map( (d) ->
m = {}
m['id'] = new Date(0)
m['id'].setUTCSeconds( d[1] )
m['val'] = d[0]
m
)
undefined
as you can see, i overload the id to be the timestamp of each Measurement and all measurements for a specific 'metric' is stored under a Collection called Measurements.
i've also hardcoded it so it really only works for one Measurements Collection (ie the 'target' in the json).
my question concerns how best/elegant/flexible to implement the gathering of multiple Measurements (the collection) in a single call. ie graphite supports the use of wildcards for its 'targets' so that an ajax request to /?target=some.string.*
would bring back all matching targets and datapoints (like in the json example). i would then present this to a View where i would render say cumulative data or plot all of the Measurements against time.
i was thinking of using another Collection (lets call it a Set) that would contain many Measurements. I would like to be able to do something like Set.fetch()
to get all the matching Measurements from the server and have the Set create many Measurements Collections from the single ajax request.
does anyone have any suggestions of how to implement this? or even a better way of representing this model/collection layer?