1

We have a app with, among other stuff, lists of things. In this web app, we have a custom jQuery plugin, that we apply to the lists, which shows a context menu for the clicked table row. In some cases, it makes sense to let the user interact with related objects through these context menus, for example clicking on a row in a list of projects, and selecting edit project manager, should take me to the edit user page for the user that's assigned as the project manager to that project.

Within an AngularJS app, this is quite simple: clicking the context menu just takes me to the route corresponding to the action I want to take, and routing handles the rest.

However, this is a (very...) large web application, and only small parts of it are written with AngularJS; we also need to support custom callback functions from the context menu. Sometimes we want to open a popup window with a different url, sometimes we want to make a custom AJAX call, sometimes we want to do something else...

The configuration of the context menu - what elements are displayed etc - is done on the server, and passed to the client as JSON. Thus, I can't specify a callback function on the configuration object directly, but I'm limited to strings and other JSON-representable things.

I had hoped to be able to do this simply by configuring a route and saying "when I go to this route, call this here function and abort navigation", and then specify a url template on the config object. However, this doesn't seem trivial, and all attempts I can find seem to be deeply interlinked with navigating to a specific controller and/or view somewhere; I haven't found anything that helps me understand how to perform a general JavaScript action based on a route.

Is there a way to use Angular routing to call general callback functions, without any navigation?


UPDATE

Given the comments, I'm a little worried that it might have been better to state this problem differently, so here is another try:

Given a JSON object that I fetch from the server (note: not a full-fledged javascript object - no properties can be functions, for example), how can I map a property on the object to a function call on the client, using as little custom code as possible (in other words, using Angular for as much as possible)?

Community
  • 1
  • 1
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • Can you not just set up a method in the controller you are using? I use the ui-router and am able to set up a 'resolve' method on the state/view change that works great for this purpose. – beauXjames Nov 05 '14 at 16:08
  • @beauXjames: We're using the regular `ngRoute` module - am I understanding you correctly that I need to switch to [this project](https://github.com/angular-ui/ui-router) to get this working? I'm a little reluctant to do so, because of this quote from their readme: `**Note:** UI-Router is under active development. As such, while this library is well-tested, the API may change. Consider using it in production applications only if you're comfortable following a changelog and updating your usage accordingly.` – Tomas Aschan Nov 05 '14 at 16:26
  • there may be other ways to go about solving your problem...ui-router happens to be a pretty simple migration, though...I understand the 'under development' clause, but what isn't these days? – beauXjames Nov 05 '14 at 17:29
  • did you try http interceptors? – Petr Averyanov Nov 05 '14 at 18:47
  • @PetrAveryanov: I'm not sure http interceptors will solve this problem either, since what I want to intercept this long before it comes to the $http service. If I'm wrong, feel free to write up an answer that shows me why! ;) – Tomas Aschan Nov 06 '14 at 07:57

0 Answers0