Note that there are some explanatory texts on larger screens.

plurals
  1. POBackbone Model triggers event conditionally, View doesn't hear it
    text
    copied!<p>I'm creating a Geolocation Model, fetching from localStorage, and checking if I have there is a latitude property there. If there isn't, a <code>'geolocation:city_state_blank'</code> event is triggered. If <code>'geolocation:city_state_blank'</code> is heard, <code>@get_users_geolocation()</code> is fired.</p> <pre><code>class App.GeoLocation extends Backbone.Model initialize: -&gt; @on 'geolocation:city_state_blank', -&gt; @get_users_geolocation() @fetch().always =&gt; if @get('city_state').length is 0 @trigger 'geolocation:city_state_blank' else @trigger('geolocation:done') get_users_geolocation: =&gt; # Code to get / save (in localStorage) a users geolocation and city / state @trigger('geolocation:done') </code></pre> <p>After <code>get_users_geolocation()</code> is done the event <code>geolocation:done</code> is triggered. </p> <p>I've removed details of fetching the users geoLocation / reverse Geolocation lookups, which are all asynchronous. But the end result of all that work boils down to triggering the <code>geolocation:done</code> event.</p> <pre><code>class App.GeolocationView extends Backbone.View initialize: =&gt; @listenTo @model, 'geolocation:done', =&gt; alert('geolocation:done was heard in the view!!!!') </code></pre> <h1>Here is the problem:</h1> <p>In the scenario when the Geolocation model fetches from localStorage and determines the property <code>latitude</code> is <strong>not set</strong>, and thus calls <code>get_users_geolocation</code> — the View alerts geolocation:done was heard in the view!!!!</p> <p>But in the scenario when Geolocation has a latitude property (the else), and triggers the <code>geolocation:done</code> right away, the View <strong>does not</strong> alert anything. The view doesn't hear it.</p> <p>I've console.logged the hell out of this, and can say the flow paths are working. The if/else are working and the View is getting instantiated correctly. Logging within the callback after fetching from localStorage yields this..</p> <pre><code>@fetch().always =&gt; console.log @get('current_city_state') console.log typeof @get('current_city_state') // Norfolk, VA // String </code></pre> <p>So, there is data there..</p> <p>What is going on?? Help please!!</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