Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert [object Object] in Backbone to actual data
    primarykey
    data
    text
    <p>I have a function in my Backbone view(file.js) which is populated with a JSON response that's in the below format:-</p> <p><strong>Function in Backbone View</strong>:-</p> <pre><code>chart_data: function(){ var all_stats = this.collection.toJSON(); $("#page-container").html(this.users_by_loc({ platform_stats: all_stats, graph_data: all_stats.items.slice(0,5) // I want to use the top 5 entries to plot graph })); } </code></pre> <p><strong>Response</strong> present as part of <strong>all_stats</strong>(from my api call - I'm using Rails on server side):-</p> <pre><code>{ "items": [ { "loc": "Chennai", "users": 11707 }, { "loc": "Hyderabad", "users": 4327 }, { "loc": "Pune", "users": 3625 },....(many more entries) ] } </code></pre> <p>Now, I'm trying to pass the json response present in the <strong>graph_data</strong> parameter(defined in my <strong>Backbone view</strong> above) to a variable called <strong>chartData</strong> which is defined in my <strong>jst.ejs</strong> template(code below) . This variable(chartData) is used by my AM charts JS code to actually plot the graph.</p> <p>My <strong>jst.ejs</strong> file which has the code to populate the graph data and plot the graph looks like this:-</p> <pre><code>&lt;script&gt; var chart; chartData = "&lt;%= graph_data %&gt;" console.log("chartData: ", chartData); AmCharts.ready(function () { // SERIAL CHART chart = new AmCharts.AmSerialChart(); chart.dataProvider = chartData; //additional code to help plot the chart chart.write("chartdiv"); }); &lt;/script&gt; &lt;div id="chartdiv" style="width: 60%; height: 400px;"&gt;&lt;/div&gt; </code></pre> <p>My <strong>current roadblock</strong> is that when I do..</p> <pre><code>chartData = "&lt;%= graph_data %&gt;" console.log("chartData: ", chartData); </code></pre> <p>.. in my jst.ejs template the chartData variable instead of having the actual data as part of <strong>all_stats</strong> (shown_above) returns the following in my firebug console:-</p> <pre><code>chartData: [object Object],[object Object],[object Object],[object Object],[object Object] </code></pre> <p>How do I <strong>correctly assign</strong> the actual data present in <strong>graph_data</strong> variable(as part of my <strong>backbone view</strong>) to the variable <strong>chartData</strong> present in my <strong>jst.ejs</strong> template to plot the graph ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload