模块化现场上下文感知AJAX调用上下文、现场、AJAX

2023-09-10 19:15:31 作者:男生够帅才梳得起中分

编辑:我想到了一个可能的解决方案,但我做了另外一个问题,因为它是非常具体:看的用PHP AJAX代理,这可能吗? 有几次,我遇到了这个问题...

I thought about a possible solution, but I made another question as it is very specific: see AJAX proxy with PHP, is it possible? A couple of times I've encountered this problem...

我创建具有一定程度的模块化位点。因此,很可能有分量(想到一个粗略的CMS),它携带自己的PHP code,CSS和JavaScript,所有动态包括在内。想想一个结构类似:

I create sites that have a certain degree of modularity. So, it is possible that there are "components" (think of a rough CMS) which carry their own PHP code, CSS, and JavaScript, all dynamically included. Think about a structure like:

{siteroot}/component/datagrid/datagrid.php
{siteroot}/component/datagrid/js/datagrid.js
{siteroot}/component/datagrid/css/datagrid.css
{siteroot}/component/datagrid/ajax/getsomedata.php

现在的问题是:作为JavaScript文件,并expecially AJAX调用,如何使他们的上下文感知的网址? 例如,如果在 datagrid.js 我要打电话 siteroot /组件/数据网格/ AJAX / getsomedata.php 与AJAX我应该写(使用jQuery):

Now, the question is: for JavaScript files, and expecially AJAX calls, how do I make them context-aware with the URLs? For example, if in datagrid.js I want to call siteroot/component/datagrid/ajax/getsomedata.php with AJAX I should write (with JQuery):

$("#ajax").load("siteroot/component/datagrid/ajax/getsomedata.php");

第一个问题:在不同的安装 siteroot 的变化。我已经成功通过包括广播

First problem: siteroot changes on different installations. I've managed that by including a general

var codeBase = <? echo json_encode(Config::$siteRoot); ?>

用PHP每一页上,从可轻松编辑,每次安装配置文件,这样我就可以做任何的JavaScript是这样的:

with PHP on every page, from a Config file that can be easily edited for every installation, so I can do with whatever JavaScript something like:

$("#ajax").load(codeBase + "/component/Datagrid/ajax/getsomedata.php");

你怎么想到这个办法的? 问题二:但我有PHP函数返回我还组件文件夹,或其他组件的文件夹。这将是很好使的全 URL动态。这也将发生的变化的组件的结构,如果我想。 我发现的唯一的解决办法是使用 .js.php 动态JavaScript。这是非常unelegant,我必须包含的JavaScript文件中的所有框架,如:

What do you think of this approach? Second problem: but I have PHP functions that return to me also the components folder, or the folder of other components. It would be nice to make the whole URL dynamic. This would account also for changes in the structure of the component if I want. The only solution I've found is to use a .js.php dynamic Javascript. This is very unelegant, and I have to include all the framework in the JavaScript file, like:

<?php
include "../../libs/framework.php"; // get my functions...
$myUrl = Config::$siteRoot . Framework::getComponentAjaxDir("datagrid") . "/getsomedata.php";
?>
$("#ajax").load(<?=json_encode($myUrl)?>);

另一个副作用是,我要知道的完全的包括的路径 framework.php ...我不希望这样呢硬CDIN我的.js.php文件$ C $。 有关任何智能的解决方案?

Another side effect is that I have to know exactly the include the path for framework.php... I don't want this so hard-codedin my ".js.php" file. Any smart solutions about that?

推荐答案

由于没有人以适当的方式回答,我回答自己提供,我发现,可能是有用的解决方案。 的关键,我的解决方法很简单:

As nobody answered in a suitable way, I answer to myself to provide a solution I've found out that can be useful. The key to my solution is simple:

在我创建在我的网站结构的固定位置的AJAX代理,这样我就可以使用 codeBase的从JavaScript引用代理

我叫有两个参数这个代理:插件动作,其识别)的插件夹在其中的真正的Ajax是和b)在AJAX文件中使用,连同其他PARAMS: I create an AJAX proxy at a fixed location in my site structure, so I can use codeBase to reference the proxy from JavaScript

I call this proxy with two parameters: plugin and action, which identify a) the plugin folder in which the "real" ajax is and b) the ajax file to use, along with the other params:

$(#......)。载荷(codeBase类+/main/ajax.php?plugin=Datagrid&action=gettable&otherparams......)

$("#...").load( codeBase + "/main/ajax.php?plugin=Datagrid&action=gettable&otherparams"...)

ajax.php 我消毒参数,并使用插件动作获得真正的AJAX文件:

In ajax.php I sanitize the parameters, and use plugin and action to obtain the "real" ajax file:

{}的ServerRoot /组件/插件{} / AJAX / {行动}的.php

{serverRoot}/components/{plugin}/ajax/{action}.php

然后,我只是包括 ajax.php