Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I do a redirect to a path with an object in ember?
    text
    copied!<p>I'm trying to put together a data-driven menu system in Ember.js. Here's the javascript, <code>js/nodal.js</code>:</p> <pre class="lang-js prettyprint-override"><code>window.Form = Ember.Application.create(); var menuOptions = [ {id: "root", options: [ "a", "b" ]}, {id: "a", name: "Option A", options: [ "c", "d" ]}, {id: "b", name: "Option B", options: [ "e", "f" ]}, {id: "c", name: "Option C"}, {id: "d", name: "Option D"}, {id: "e", name: "Option E"}, {id: "f", name: "Option F"} ] Form.MenuOption = Ember.Object.extend({}); var builtOptions = menuOptions.map( function(attrs) { return Form.MenuOption.create( attrs ); }); Form.options = {}; for( i = 0; i &lt; builtOptions.length; i++ ) { var opt = builtOptions[i]; Form.options[opt.id] = opt; } Form.MenuRoute = Ember.Route.extend({ model: function(params){ console.log( params ); if( !params || !params.index_id || params.index_id == "undefined") { params.index_id = "root"; } var keys = Form.options[params.index_id].options; var options = new Array(); keys.map(function(k){ options.push( Form.options[k] ); }); return options; } }); Form.IndexRoute = Ember.Route.extend({ redirect: function(){ /* This here is the problem line... */ this.transitionTo( "menu", Form.options.root ); } }); Form.Router.map(function(){ this.resource( "menu", { path: "/menu/:index_id" } ); }); </code></pre> <p>And here's the HTML, such as it is:</p> <p> </p> <pre><code> &lt;title&gt;Hello&lt;/title&gt; &lt;meta name="description" content="My First Ember.js App"&gt; &lt;meta name="author" content="Your Name Here"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;/head&gt; &lt;body&gt; &lt;script data-template-name="application" type="text/x-handlebars"&gt; &lt;h1&gt;Title Goes Here&lt;/h1&gt; {{outlet}} &lt;/script&gt; &lt;script data-template-name="menu" type="text/x-handlebars"&gt; &lt;ul&gt; {{#each controller}} &lt;li&gt;{{#linkTo menu this}}{{name}}{{/linkTo}}&lt;/li&gt; {{/each}} &lt;/ul&gt; &lt;/script&gt; &lt;script src="js/libs/jquery-1.9.1.js"&gt;&lt;/script&gt; &lt;script src="js/libs/handlebars.js"&gt;&lt;/script&gt; &lt;script src="js/libs/ember-1.0.0-rc.1.js"&gt;&lt;/script&gt; &lt;script src="js/nodal.js"&gt;&lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>What I want is for the user to be redirected to <code>/menu/root</code> as soon as they open the page. So far I haven't been able to find the right incantation to make this work. When I load the above, I get this exception:</p> <pre><code>Error: assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed &lt;(generated menu controller):ember172&gt; </code></pre> <p>If I load <code>index.html#/menu/root</code> directly, it works - it's only the redirect that's not working. Replacing the <code>this.transitionTo</code> line in the IndexRoute definition with this:</p> <pre class="lang-js prettyprint-override"><code>this.transitionTo( "menu", [Form.options.root] ); </code></pre> <p><em>does</em> redirect, but it redirects to <code>/menu/undefined</code>, not <code>/menu/root</code>. That's ugly, and obviously broken.</p> <p>What have I missed?</p> <p><strong>EDIT</strong></p> <p>As suggested, I tried adding an ArrayController:</p> <pre><code>Form.MenuController = Ember.ArrayController.extend({}); </code></pre> <p>This gives me a new error:</p> <pre><code>arrangedContent.addArrayObserver is not a function </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