codeforamerica / fast_pass

Las Vegas Development Opportunity Finder
BSD 3-Clause "New" or "Revised" License
7 stars 4 forks source link

Communicate mapbox.js lat/lng to Angular controllers #34

Closed louh closed 11 years ago

louh commented 11 years ago

So I can do something in plain Javascript like this:

map.on('click', function (e) {
    console.log('Clicked map lat/lng: ' + e.latlng)
    // Returns 'Clicked map lat/lng: LatLng(36.17419, -115.14865)'
}

Basically, getting Lat/Lng points out of Mapbox/Leaflet is pretty easy. But now, how do I communicate this to an Angular controller?

Most of Angular relies on binding directly to DOM elements via attributes, which would mean interaction with a map element would bypass mapbox.js completely, as far as I understand. I'm not sure I've ever seen anything about how Angular scopes are accessible outside of their controllers, which I imagine would be anti-Angular anyway (because of possible scope pollution).

cc @rclosner

lovehandle commented 11 years ago

@louh haven't looked into this yet, but would it make sense to have two hidden input elements that you bind to instead? then you could have an 'on-click' map event that populates those inputs (which are bound to the controller).

louh commented 11 years ago

Interesting. This seems like a pretty effective solution; let me give that a try.

louh commented 11 years ago

...so close. when jQuery updates the input boxes, it doesn't automatically update Angular (only human interaction does that) - there is apparently a way to force Angular to look for changes, but have to look into it more.

lovehandle commented 11 years ago

@louh want to post your changes to the branch and link to the line of code you're working on? would this work:

jQuery(element).bind('change', function () {
  angular.element(element).triggerHandler('change');
})

a little hacky, but it may do the trick.

louh commented 11 years ago

@rclosner That didn't seem to work. I tried a bunch of other hacky things, like:

Finally I tried various forms of:

THAT WORKED. I was thinking too hard. Essentially, the solution is to also bind a click event to the controller, then, during the callback function, manually assigning the input fields to the model.

Whew!!!!