Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js: showing multiple database-backed resources/models in the same page/route?
    text
    copied!<p>I'm writing a small test app using Ember, in the form of a budget manager. I have a Budget object, which contains general properties like the monthly limit and a description. I also have a set of Expense objects, which contain a name, the amount spent, etc. Both are retrieved from a server using Ember Data's REST adapter.</p> <p><strong>HTML:</strong></p> <pre><code>&lt;body&gt; &lt;script type="text/x-handlebars" data-template-name="budget"&gt; &lt;h2&gt;{{name}} (€ {{amount}})&lt;/h2&gt; &lt;/script&gt; &lt;script type="text/x-handlebars" data-template-name="expenses"&gt; &lt;ul id="expense-list"&gt; {{#each model}} {{render "expense" this}} {{/each}} &lt;/ul&gt; &lt;/script&gt; &lt;!-- expense template --&gt; &lt;script type="text/x-handlebars" id="expense"&gt; &lt;li&gt; &lt;label&gt;{{description}}&lt;/label&gt; &lt;label class="subtle"&gt;{{formatDate time}}&lt;/label&gt; &lt;label class="amount"&gt;{{amount}}&lt;/label&gt; &lt;/li&gt; &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>JavaScript</strong>:</p> <pre><code>window.App = Ember.Application.create(); App.ApplicationAdapter = DS.RESTAdapter.extend({ host: 'http://localhost:5000', namespace: 'outthepocket/api' }); // Model App.Expense = DS.Model.extend({ amount: DS.attr('number'), description: DS.attr('string'), time: DS.attr('date') }); App.Budget = DS.Model.extend({ name: DS.attr('string'), amount: DS.attr('number') }); // Routes App.Router.map( function() { this.resource('budget'); this.resource('expenses'); }); App.ExpensesRoute = Ember.Route.extend({ model: function() { return this.store.find('expense'); } }); App.BudgetRoute = Ember.Route.extend({ model: function() { return this.store.find('budget', 1); } }); </code></pre> <p>Following the architecture I see in all the Ember tutorials, there is an ExpensesRoute with the list of expenses as its model, and a BudgetRoute with the selected budget as its model. This works great as long as I go through the proper URL to see each resource:</p> <ul> <li><strong>myapp.html#budget</strong> renders the budget template with data from the server.</li> <li><strong>myapp.html#expenses</strong> renders the expenses template with data from the server.</li> </ul> <p>The problem I'm having now is that I want to <strong>display both templates, with their data, on one page</strong> (the index page). I've tried two solutions for this:</p> <ul> <li><p><strong>Solution 1</strong>: Have separate routes and templates and call {{render budget}} and {{render expenses}} in the main application template. This renders both templates, but without any data.</p></li> <li><p><strong>Solution 2</strong>: Have just an IndexRoute and return both budget and expenses from its model property, rendering them into the index template. This more or less works, but seems counter to Ember's otherwise nice separation of different resources, routes and controllers.</p></li> </ul> <p>Any thoughts? I've been through five or six tutorials and Ember's official guide, but none of those have made clear how to assemble a one-page web app with multiple templates backed by multiple resources without having to link to different pages/routes.</p>
 

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