Skip to content

Instantly share code, notes, and snippets.

@efeminella
Created April 3, 2012 22:10
Show Gist options
  • Save efeminella/2295844 to your computer and use it in GitHub Desktop.
Save efeminella/2295844 to your computer and use it in GitHub Desktop.
A Highcharts Data Point Adapter for Backbone Models
/*
* Defines a Model which adapts the representation of the relationship
* between two variables defined as x and y, to specific named properties
* which represent 'x' and 'y'.
*
* For example, a data point which models Temperature may represent time
* on the x-axes as x, and degrees on the y-axes as y. A Model can extend
* DataPoint to map x and y to their corresponding named properties defined
* as time and degrees, respectively.
*
* // Maps the data point coordinate x to the property month, and the data
* // point coordinate y to the property degrees.
* var Temperature = Backbone.DataPointAdapter.extend(
* {
* defaults : {
* 'x' : 'month',
* 'y' : 'degrees'
* }
* });
*
*/
Backbone.DataPointAdapter = Backbone.DataPointAdapter || Backbone.Model.extend(
{
/*
* By default, the 'x' and 'y' coordinates map to generic x and y
* values. Implementations are to override these defaults in order
* to specify the specific named properties which map to the x and
* y coordinates of a Data Point.
*/
defaults : {
'x' : 'x'
, 'y' : 'y'
},
/*
* Returns the mapped property names specified for 'x' and 'y'
* as a data point consisting of the values defined by the named
* properties mapped as x and y.
*/
toDataPoint: function() {
return _.extend( {}, this.attributes, {
'x': this.get( this.get('x') )
, 'y': this.get( this.get('y') )
});
},
/*
* Returns the given data point as an object mapped to the specified
* named properties which correspond to 'x' and 'y' in this model.
*/
fromDataPoint: function(dataPoint) {
var values = {};
values[ this.get('x') ] = dataPoint.x;
values[ this.get('y') ] = dataPoint.y;
return values;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment