Note that there are some explanatory texts on larger screens.

plurals
  1. POdisplaying data after ajax call in Ember
    primarykey
    data
    text
    <p>One of the founders of Discourse has posted an entry on his blog <a href="http://eviltrout.com/2013/02/27/adding-to-discourse-part-1.html" rel="nofollow">http://eviltrout.com/2013/02/27/adding-to-discourse-part-1.html</a> explaining how he makes ajax queries in Ember (as opposed to using Ember data). If the user navigates to a route it will trigger a found method</p> <p>Route</p> <pre><code> Discourse.AdminReportsRoute = Discourse.Route.extend({ model: function(params) { return(Discourse.Report.find(params.type)); }, </code></pre> <p>Find method</p> <pre><code>Discourse.Report.reopenClass({ find: function(type) { var model = Discourse.Report.create(); jQuery.ajax("/admin/reports/" + type, { type: 'GET', success: function(json) { model.mergeAttributes(json.report); model.set('loaded', true); }, }); return(model); } }); </code></pre> <p>I got this to work using a different example, however, in the success callback I get this error message</p> <pre><code>Uncaught TypeError: Object [object Object] has no method 'mergeAttributes' </code></pre> <p>As I didn't have Discourse set up, I created a different domain model</p> <pre><code>App.Restaurant = Ember.Object.extend({}); App.Restaurant.reopenClass({ find: function(type) { var model = App.Restaurant.create(); jQuery.ajax("restaurants/",{ type: 'GET', success: function(json) { console.log(json); model.mergeAttributes(json.restaurants); model.set('loaded', true); }, }); return(model); } }); </code></pre> <p>Logging the json in the success callback showed this</p> <pre><code>Object {restaurants: Array[28]} restaurants: Array[28] __proto__: Object </code></pre> <p>Why isn't mergeAttributes defined on my model?</p> <p>I tried to remove model.mergeAttributes and just do</p> <pre><code> success: function(json) { model.set('loaded', true); }, </code></pre> <p>I get this error</p> <pre><code>Assertion failed: The value that #each loops over must be an Array. You passed &lt;App.Restaurant:ember322&gt; </code></pre> <p>So if I can't do mergeAttributes, is there a way to make the data returned in the success callback an array?</p> <pre><code>{{#if loaded}} &lt;ul&gt; {{#each item in model}} &lt;li&gt;{{item}}&lt;/li&gt; {{/each}} &lt;/ul&gt; {{else}} {{ loading}} {{/if}} </code></pre> <p>--</p> <pre><code> model: function(params) { return(App.Restaurant.findAll(params)); }, renderTemplate: function() { this.render('restaurants', {into: 'application'}); } </code></pre>
    singulars
    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.
    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