Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For this job you should go with <code>javascriptRoutes</code> as it generates correct JS paths based on your routes.conf. You'll find usage sample in Zentask <a href="https://github.com/playframework/Play20/tree/master/samples/">sample</a></p> <p>Anyway, for now you can fix your AJAX call by changing the <code>url</code> to</p> <pre><code>url : '@routes.Application.saveDefaultPhoneForUser()', </code></pre> <p>This way requires it to place the whole JS in template, which is wrong. It can or even should be moved to separate JS file and to make it possible you need to use javascriptRoutes.</p> <p><strong><em>More...</em></strong></p> <p><strong>javascriptRoutes</strong> are not described <strong><em>yet</em></strong> in official documentation, but here's step-by-step introduction to it. Although the description looks sophisticated <em>de facto</em> using this way brings a lot of benefits.</p> <h2>1. Create the common routes</h2> <p>First you need to create common routes in <code>conf/routes</code> file:</p> <pre><code>GET /item/:id controllers.Application.getItem(id: Long) POST /item/new controllers.Application.newItem PUT /item/:id controllers.Application.updateItem(id: Long) </code></pre> <p>Of course, you need to create at least these three actions in <code>Application</code> controller:</p> <ul> <li><code>getItem(Long id){ ... }</code></li> <li><code>newItem() { ... }</code></li> <li><code>updateItem(Long id) { ... }</code></li> </ul> <h2>2. Create an action translating common routes to JS</h2> <ul> <li>place it somewhere, ie. in your <code>Application</code> controller</li> <li>Let's call it <code>javascriptRoutes()</code></li> </ul> <p>In that action you'll point the <strong>existing</strong> routes from the <code>conf/routes</code> file</p> <pre><code>public static Result javascriptRoutes() { response().setContentType("text/javascript"); return ok( Routes.javascriptRouter("myJsRoutes", routes.javascript.Application.getItem(), routes.javascript.Application.newItem(), routes.javascript.Application.updateItem(), //inside somepackage controllers.somepackage.routes.javascript.Application.updateItem() ) ); } </code></pre> <p><strong><em>Note:</em></strong> Don't set any params in brackets.</p> <h2>3. Create a route for <code>javascriptRoutes</code> action and include it in your template</h2> <p>Route <code>conf/routes</code></p> <pre><code>GET /javascriptRoutes controllers.Application.javascriptRoutes </code></pre> <p>View in <code>&lt;head&gt;</code> part of <code>/views/main.scala.html</code></p> <pre><code>&lt;script type="text/javascript" src='@routes.Application.javascriptRoutes()'&gt;&lt;/script&gt; </code></pre> <h2>4. Use javascriptRoutes where you want</h2> <p>Up from now you can use routes in JS to get the correct path without need to specify the <code>url</code> and <code>type</code>. For an example instead of:</p> <pre><code> $('.getAjaxForThisContainer').click(function(e) { var idToGet = $("#someField").val(); $.ajax({ type : 'GET', url : '@routes.Application.getItem()', data : { id: idToGet }, success : function(data) { // ... some code on success } }); return false; }); </code></pre> <p>you can use simplified version (<code>myJsRoutes</code> from point 2):</p> <pre><code>myJsRoutes.controllers.Application.getItem(idToGet).ajax({ success : function(data) { ... some code ... } }); </code></pre> <p>or </p> <pre><code>myJsRoutes.controllers.Application.newItem().ajax({ success : function(data) { ... some code ... } }); </code></pre> <p>etc...</p> <ul> <li>you don't need to specify <code>type: "POST"</code> - JS router will use correct method according to <code>conf/routes</code> rule</li> <li>you can set <code>id</code> of the record (or other params) to <code>GET</code> or <code>PUT</code> (or other methods) using <code>routes-like</code> syntax in pure JS </li> <li>If your route rule contains all required params you can really minimize yours JS:</li> </ul> <p>for route:</p> <pre><code>GET /some/:a/:b/:c controllers.Application.getABC(a: String, b: Integer, c: String) </code></pre> <p>JS:</p> <pre><code>myJsRoutes.controllers.Application.getABC("a", 1, "b" ).ajax({}); </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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