执行基于路线在角(没有导航)的回调函数(Executing a callback function

2019-10-21 05:35发布

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)?

文章来源: Executing a callback function based on routes in angular (no navigation)