3

I want a function which does some calculations to be called each time the route is edited.
The route_has_been_edited event is emitted only by an existing route (pitily).
I connect the event of deselecting routeEditor control (which means the route has just been created) to a function call.
This nameless function creates a connection between routeupdate event in the freshly made route and the calculating function.
Here is my noob attempt to code it

routeEditor.events.add ('deselect', function (e) {
    var route = e.get('target').getRoute();
    alert ('Route length - ' + route.getLength());
    // notice me, senpai
    route.Editor.events.add ('routeupdate', recalc(polygon, route));
    recalc (polygon, route);
    alert("Watashi o mushi shinaide kudasaaai (>_<)");
});

I didn't check if I have done the second event connection right, but even despite that, why does the first alert work, and second does not?

mekkanizer
  • 722
  • 2
  • 9
  • 28
  • Is the console showing any errors? – mwilson Sep 18 '15 at 00:12
  • @mwilson that is the strangest part. Not even a warning – mekkanizer Sep 18 '15 at 00:12
  • Can you put together a JSFiddle of the complete code or reproduction of the issue? – mwilson Sep 18 '15 at 00:14
  • Does the function `recalc()` return a function? Because in your `routeUpdate` event, you're **calling** `recalc`, instead of passing it. – azium Sep 18 '15 at 00:16
  • @mwilson is a github link satisfactory? https://github.com/mekkanizer/taxi-dispatcher/blob/master/test/test.js – mekkanizer Sep 18 '15 at 00:17
  • In fact, I'm certain it does not, looking at it again. You probably want to call `recalc(polygon, route)` inside the callback. Such as `events.add('routeupdate', function () { recalc(polygon, route) })` – azium Sep 18 '15 at 00:18
  • @azium is the solution to write `recalc`? Without arguments? – mekkanizer Sep 18 '15 at 00:18
  • Also it looks like you're adding the `routeupdate` event on EVERY `deselect` event fire.. are you sure you don't want to hookup that event handler just one time? – azium Sep 18 '15 at 00:20
  • @azium whooops I guess you're right. Of course, I want to hook it only once. And pitily, neither deleting arguments nor using your code helps – mekkanizer Sep 18 '15 at 00:22

1 Answers1

0

Looks like a typo that's causing JS to error out (even though you're not seeing it, maybe errors are off?).

Change:

route.Editor.events...

To:

routeEditor.events...

If that's not a typo, try commenting out the line with route.Editor and confirm that both alerts fire. It could be that recalc is also causing a JS crash.

Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • The names seem to be confusing. Let me tell you the difference between `routeEditor` and `route.Editor`. The first one is a [control](https://tech.yandex.ru/maps/doc/jsapi/2.1/ref/reference/control.RouteEditor-docpage/), and the second one is an [event emitter](https://tech.yandex.ru/maps/doc/jsapi/2.1/ref/reference/router.Editor-docpage/#events-summary). – mekkanizer Sep 18 '15 at 11:52
  • However, my friend suggested just erasing `.Editor`, and the second alert started working, now I have to figure out if the event hook *does* work with those edits – mekkanizer Sep 18 '15 at 11:54
  • The difference is that `routeEditor` is in the closure where as `route` is returned from the `getRoute()` method which might not be working as you expect. If the object is following javascript conventions the property on `route` should be should be `route.editor` not `route.Editor`. If my above suggestion works you should accept this answer to clear it from the unanswered queue. – Dane O'Connor Sep 18 '15 at 14:07
  • ok, i finally figured everything. solution is three things: * change `route.Editor.events` to simply `route.events`; * add `function () { ` before the second argument of second `events.add`; * remove `return function () {` from the `recalc` method declaration line – mekkanizer Sep 23 '15 at 15:03