I’m creating a bar graph using Morris.js. What I’m showing in the graph is dates on the X-axis and the number of times an event has occurred on that date on the Y-axis.
I created a class method in my model that fetches all records, groups by date and count the occurrences. This method is called by a helper method from my view. The helper method loops over the date range and checks the number of occurrences for that date and adds it to a hash (dates that doesn’t exist in the database is set to 0).
This hash is then simply added to my view as a data attribute (data-events=“{…}”) which I’m fetching using JS and adding to the graph.
The questions I have are:
Is this a good approach?
Would it be better to add another action to my controller and have that return the data needed as JSON and call just this using javascript?
What are the benefits/drawbacks of the different approaches?
Another option would be to fetch the raw JSON data using javascript from …/events.json and then do the grouping and whatever client-side and pass to the graph… Maybe too inefficient?
I created a class method in my model that fetches all records, groups by
date and count the occurrences. This method is called by a helper method
from my view. The helper method loops over the date range and checks the
number of occurrences for that date and adds it to a hash (dates that
doesn't exist in the database is set to 0).
This hash is then simply added to my view as a data attribute
(data-events="{....}") which I'm fetching using JS and adding to the
graph.
The questions I have are:
- Is this a good approach?
- Would it be better to add another action to my controller and have
that
return the data needed as JSON and call just this using javascript?
What are the benefits/drawbacks of the different approaches?
Generally speaking try to avoid pre-mature optimization.
1. Go with the simplest solution that could possibly work.
2. Gather some metrics.
3. Try a different approach that you think might improve performance.