Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the right approach to reload the model for a non-dynamic route with ember.js?
    text
    copied!<p>I'm having a simple array of models which I display in a list (path: /things). The models get loaded from a REST-API. </p> <p>In a nested route I have the functionality to add a new model. (path: /things/add). The new model is persisted over a REST-API.</p> <p>After adding the new model, I do a <code>transitionTo('things')</code> to get back to the list.</p> <p>Following the ember documentation, by using "transitionTo" neither the <code>model</code> hook nor the <code>setupController</code>-Hook are called for non dynamic routes. </p> <p><strong>What is the right approach to refresh the model on a non-dynamic route, when using <code>transitionTo</code>? Or: what is the best way to reload a model on a non-dynamic route without using <code>transitionTo</code>?</strong></p> <h3>app.js</h3> <pre><code>// App Init App = Ember.Application.create(); // Routes App.Router.map(function() { this.resource('things', function() { this.route('add'); }) }); App.IndexRoute = Ember.Route.extend({ redirect : function() { this.transitionTo('things'); } }); App.ThingsRoute = Ember.Route.extend({ model : function(param) { return App.Thing.findAll(); }, }); App.ThingsAddRoute = Em.Route.extend({ setupController : function(controller) { controller.set('content', App.Thing.create()); } }); // Models App.Thing = Ember.Object.extend({ name : null, description : null }); App.Thing.reopenClass({ findAll : function() { var result; $.ajax({ url : 'http://path/app_dev.php/api/things', dataType : 'json', async : false, success : function(data) { result = data.things; } }); return result; }, save : function(content) { console.log(content); $.ajax({ type : 'post', url : 'http://path/app_dev.php/api/things', data : { name : content.name, description : content.description }, async : false }); } }); // Controller App.ThingsAddController = Em.ObjectController.extend({ add : function() { App.Thing.save(this.content); this.transitionToRoute('things'); }, cancelAdding : function() { this.transitionToRoute('things'); } }); </code></pre> <h3>index.html</h3> <pre><code>&lt;script type="text/x-handlebars"&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="span12"&gt; &lt;h1&gt;List of things&lt;/h1&gt; &lt;/div&gt; {{outlet}} &lt;/div&gt; &lt;/div&gt; &lt;/script&gt; &lt;script type="text/x-handlebars" data-template-name="things/add"&gt; &lt;div class="span12"&gt; &lt;form class="form-horizontal"&gt; &lt;fieldset&gt; &lt;div id="legend"&gt; &lt;legend class=""&gt;Add new thing&lt;/legend&gt; &lt;/div&gt; &lt;!-- Name --&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="name"&gt;Name&lt;/label&gt; &lt;div class="controls"&gt; {{view Ember.TextField id="name" placeholder="Enter Name" valueBinding="name"}} &lt;/div&gt; &lt;/div&gt; &lt;!-- Description --&gt; &lt;div class="control-group"&gt; &lt;label class="control-label" for="description"&gt;Description&lt;/label&gt; &lt;div class="controls"&gt; {{view Ember.TextArea id="description" placeholder="Enter description" valueBinding="description"}} &lt;/div&gt; &lt;/div&gt; &lt;!-- Submit --&gt; &lt;div class="control-group"&gt; &lt;div class="controls"&gt; &lt;button class="btn btn-success" {{action add}}&gt;Save&lt;/button&gt; &lt;button class="btn" {{action cancelAdding}}&gt;Cancel&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;/script&gt; &lt;script type="text/x-handlebars" data-template-name="things"&gt; &lt;div class="span12"&gt; &lt;div class="btn-toolbar"&gt; &lt;div class="btn-group"&gt; {{#linkTo things.add}}&lt;i class="icon-plus"&gt;&lt;/i&gt; add new thing{{/linkTo}} &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; {{outlet}} &lt;div class="span12"&gt; &lt;table cellpadding="0" cellspacing="0" border="0" class="table table-striped "&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;ID&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Description&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; {{#each item in model}} &lt;tr&gt; &lt;td&gt;{{item.id}}&lt;/td&gt; &lt;td&gt;{{item.name}}&lt;/td&gt; &lt;td&gt;{{item.description}}&lt;/td&gt; &lt;/tr&gt; {{/each}} &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;/script&gt; </code></pre>
 

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