JavaScript Combining and extending Ractive components

I am starting to use Ractive and I would like to understand better how to make reusable components. It is quite easy to write a generic widget that can be used in different situations. My problem is that often the data in my applications does not come in the shape that the widget expects and I am not sure how to extend the widget to make it more application specific.

To make a concrete example, say I am writing a Ractive component to design a line chart. I would do something along the lines of

var Chart = Ractive.extend({
  template: chart,
  data: {
    points_to_path: function(points) {
      // whatever
    }
  }
});

with a template like

<svg>
  {{# points_to_path(points) }}
    <path d="{{ path }}" />
  {{/ end of path }}
</svg>

This component could be used like

var chart = new Chart({
  data: {
    points: [[1, 2], [3, 4], [5, 6]]
  }
});

Now assume I actually need to use such a component. Probably my data does not come in the shape of an array of two-components arrays, like points above. Maybe I have something more like

[
  {
    date: 'aug 2011',
    value: 12.51
  },
  {
    date: 'sep 2011',
    value: 12.38
  },
  ...
]

So, what I would like to do is to make a more application specific Chart that accepts data in the above form. I do not see an easy way to do this with Chart.extend, because the reference to the points variable is hardcoded in chart.html.

In a framework like Backbone, views are nested, so I could just wrap the inner widget into a bigger view and in the external view define the logic for the data transformation. I am not quite sure how to go on from here in Ractive. Maybe I should have designed the widget in a different way from the start.

More generally, my problem is that I can write small components in Ractive, but I am not quite sure how to compose them into something bigger.

What is the idiomatic way in Ractive to specialize or extend components? Or combine many components into a bigger application?

Answer:1

I'm trying to write a lightbox that shows an image inside a container when a div is being clicked. Said div has the actual image to be displayed as a background image, working as a thumbnail. These ...

I'm trying to write a lightbox that shows an image inside a container when a div is being clicked. Said div has the actual image to be displayed as a background image, working as a thumbnail. These ...

  1. getting client side value in code behind

can anyone tell me why this is not working? ui = (function() { collabElement = document.getElementById( 'approveCollab' ); if(collabElement) collabElement.onclick = function(){editor....

can anyone tell me why this is not working? ui = (function() { collabElement = document.getElementById( 'approveCollab' ); if(collabElement) collabElement.onclick = function(){editor....

I am trying to add markers via Json once a visitor moves the map. For some reason the moveend is not caught and / or the function onClickCallback is not triggered. Where am I going wrong here. ...

I am trying to add markers via Json once a visitor moves the map. For some reason the moveend is not caught and / or the function onClickCallback is not triggered. Where am I going wrong here. ...

I found this code replace(/\\r\\n/g, "<br />"); for replacing newlines into <br /> after entering strings inside a textarea. But I can't find answers on how to save it on database. My ...

I found this code replace(/\\r\\n/g, "<br />"); for replacing newlines into <br /> after entering strings inside a textarea. But I can't find answers on how to save it on database. My ...