javascript - Backbone Model triggers event conditionally, View doesn't hear it -


i'm creating geolocation model, fetching localstorage, , checking if have there latitude property there. if there isn't, 'geolocation:city_state_blank' event triggered. if 'geolocation:city_state_blank' heard, @get_users_geolocation() fired.

class app.geolocation extends backbone.model     initialize: ->         @on 'geolocation:city_state_blank', -> @get_users_geolocation()         @fetch().always =>           if @get('city_state').length 0            @trigger 'geolocation:city_state_blank'          else            @trigger('geolocation:done')         get_users_geolocation: =>         # code / save (in localstorage) users geolocation , city / state         @trigger('geolocation:done') 

after get_users_geolocation() done event geolocation:done triggered.

i've removed details of fetching users geolocation / reverse geolocation lookups, asynchronous. end result of work boils down triggering geolocation:done event.

class app.geolocationview extends backbone.view    initialize: =>       @listento @model, 'geolocation:done', =>         alert('geolocation:done heard in view!!!!') 

here problem:

in scenario when geolocation model fetches localstorage , determines property latitude not set, , calls get_users_geolocation — view alerts geolocation:done heard in view!!!!

but in scenario when geolocation has latitude property (the else), , triggers geolocation:done right away, view does not alert anything. view doesn't hear it.

i've console.logged hell out of this, , can flow paths working. if/else working , view getting instantiated correctly. logging within callback after fetching localstorage yields this..

@fetch().always =>      console.log @get('current_city_state')     console.log typeof @get('current_city_state')     // norfolk, va     // string 

so, there data there..

what going on?? please!!

i'd guess latitude property of app.geolocation single number. leave test looking this:

if some_number.length 0 

numbers don't have length properties @get('latitude').length undefined , you're left with:

if undefined 0 

that false @trigger('geolocation:done') gets called.

if you're interested in existence of latitude property, coffeescript's existential operator (?) server better:

if @get('latitude')?     # there latitude. else     # no latitude. 

to see how ? behaves have @ does:

class m extends backbone.model  m = new m(p: 0)  if m.get('p') == 0     console.log('p == 0') else     console.log('p != 0') if m.get('p')?     console.log('there p') else     console.log('the p lie') if m.get('no')?     console.log('yes no') else     console.log('no no')     

that give p == 0, there p, , no no.

demo: http://jsfiddle.net/ambiguous/qmbny/


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -