Note that there are some explanatory texts on larger screens.

plurals
  1. POEmber.js Controller & View Binding (The ember.js way...)
    primarykey
    data
    text
    <p><em>I apologize for the length of this question but I lack the insight to make it short.</em></p> <p>I am trying to learn Ember.js &amp; I am having a little trouble understanding the relationship between a controller it's view. Particularly as it relates to controller property &amp; view bindings. I have a bit of an application that <em>works</em>... But I don't really understand why &amp; I feel that <em>(rather obviously)</em> this is not the 'Ember.js' way to do it. </p> <p>Some things to consider: This application is broken out into directory/files according to what I have interpreted in the guides/documentation to be the 'correct' structure, This application is running in the context of a sinatra application. This application, although incomplete, does work the way I expect it to so far.</p> <p>The application is incomplete but here is a general overview of what I expect &amp; what I am trying to do:</p> <p>1) A user arrives at the root URL '/' and is transitioned to the '/locate' URL.</p> <p>2) The LocateController grabs the users location, populates two form fields with the lat/lng.</p> <p>3) The form is submitted to the server as POST to the sinatra '/locate' route as ajax.</p> <p><em>(request is processed by server)</em></p> <p>4) User is transitioned to the ember.js '#/located' route.</p> <p>5) Information comes back from the server as a JSON object &amp; displayed</p> <p><em>I have thus far only implemented step one. The form could be sent if I added a submit button but the idea is to have it happen automatically.</em> </p> <p><strong>The fields in the locate view are populated with the correct values for the form fields. However, I am using straight jQuery to update the values and that to me seems like it is not the 'correct' way to do this in an ember.js app</strong></p> <p>Here follows some of my code. For the sake of brevity I will just provide snippets as I feel like if you know the right answer you will be able to derive it from the provided excerpts.</p> <p>My views are set up like this: <em>I use slim in my sinatra app</em></p> <pre><code>script type="text/x-handlebars" data-template-name="application" | {{ outlet }} script type="text/x-handlebars" data-template-name="locate" | {{ message }} | &lt;form id="coordinates"&gt; | {{view Ember.TextField id="latitude" name="latitude" valueBinding="latitude" class="latitude"}} | {{view Ember.TextField id="longitude" name="longitude" valueBinding="longitude" class="longitude"}} | &lt;/form&gt; script type="text/x-handlebars" data-template-name="located" | &lt;p id="message"&gt;{{ message }}&lt;/p&gt; | &lt;p id="latitude"&gt;{{ latitude }}&lt;/p&gt; | &lt;p id="longitude"&gt;{{ longitude }}&lt;/p&gt; </code></pre> <p>script src="assets/js/app/application.js"</p> <p>applicationView.js</p> <pre><code>Application.ApplicationView = Ember.View.extend({ templateName: 'application' }); Application.LocateView = ApplicationView.extend({ templateName: 'locate' }); Application.LocatedView = ApplicationView.extend({ templateName: 'located' }); </code></pre> <p>applicationController.js</p> <pre><code>var GeoLocation; GeoLocation = (function(location){ $('#latitude').val(location.coords.latitude), $('#longitude').val(location.coords.longitude) }); navigator.geolocation.getCurrentPosition(GeoLocation) Application.ApplicationController = Ember.Controller.extend({ message: "Hello from Application" }); Application.LocateController = Ember.Controller.extend({ message: "Hello from Locate", latitude: "-- lat --", longitude: "-- lng --", }); Application.LocatedController = Ember.Controller.extend({ message: "Hello from Located", latitude: "-- lat --", longitude: "-- lng --", }); </code></pre> <p>router.js</p> <pre><code>Application.Router.map(function() { this.route('index'); this.route('locate'); this.route('located'); }); Application.ApplicationRoute = Ember.Route.extend({ events: { goToLocate: function() { this.transitionTo('locate'); }, goToLocated: function() { this.transitionTo('located'); } } }); Application.IndexRoute = Ember.Route.extend({ redirect: function() { this.render('application'); this.transitionTo('locate'); } }); Application.LocateRoute = Ember.Route.extend({ redirect: function() { this.render('locate'); //this.transitionTo('located'); } }); }); Application.LocatedRoute = Ember.Route.extend({ renderTemplate: function(controller) { this.render('located'); } }); </code></pre> <p>I have read the Guides &amp; The API Documentation on the ember.js website as well as looked at the repo on GitHub. I feel like the 'correct' implementation would use computed properties for the latitude &amp; longitude attributes but I don't quite understand how that works in practice. </p> <p>Also, I know that there is most likely a better way to deal with getting the information to the server (perhaps as JSON or ember-data when I get that far?), and feel free to share any insights you may have regarding that. But for now however, as I am comfortable with sinatra and how it works I would prefer a solution that does not require major refactoring of my backend yet. (that is to say, being able to access the location data from sinatra's params[:hash]).</p> <p>Thanks in advance for your help &amp; taking the time to read this rather long question.</p>
    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.
 

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