在flex air应用程序中处理开发/生产URL的简便方法

时间:2022-02-04 00:19:00

Easy way to handle developemnt/production URLs in flex air app? I want to point to my local box for testing, but when I launch I want it to automatically point to the production URL.

在flex air应用程序中处理开发/生产URL的简便方法?我想指向我的本地方框进行测试,但是当我启动时,我希望它自动指向生产URL。

5 个解决方案

#1


You can use namespaces and configure the current namespace (DEV/RELEASE) in your compiler options.

您可以使用命名空间并在编译器选项中配置当前命名空间(DEV / RELEASE)。

CONFIG::release
public function connect()
{
  //connect to release url
}

CONFIG::dev
public function connect()
{
   //connect to dev url
}

then define these options for the compiler:

然后为编译器定义这些选项:

-define=CONFIG::release,false
-define=CONFIG::dev,true

#2


I suggest either using a configuration file or changing your hosts file to point domains to localhost or dev servers on your development machine. With the latter option you always use your production URLs in code, but your dev machine will resolve those domains to your local machine because it checks the hosts file first.

我建议使用配置文件或更改hosts文件,将域指向开发机器上的localhost或dev服务器。使用后一种选项,您始终在代码中使用生产URL,但您的开发机器会将这些域解析为本地计算机,因为它首先检查hosts文件。

#3


The best approach here is to externalize this information into a config file - perhaps an XML file - that is loaded via a relative url. The config file might like look this:

这里最好的方法是将此信息外部化为配置文件(可能是XML文件),该文件通过相对URL加载。配置文件可能看起来像这样:

<config>
    <serviceEndpoint>http://www.mydomain.com/services</serviceEndpoint>
</config>

Be sure to name your XML elements with valid ActionScript variable names or you may encounter some difficulty working with the file (for instance, E4X expressions may become difficult.

确保使用有效的ActionScript变量名称命名XML元素,否则您可能会遇到使用该文件的一些困难(例如,E4X表达式可能会变得困难。

You can then use HTTPService to load "config.xml" which is placed alongside your application's SWF when deployed. This will allow you to repoint a SWF hosted on any domain to backend hosted anywhere else. This is especially useful if you are developing locally and are connecting to a shared development server.

然后,您可以使用HTTPService加载“config.xml”,它在部署时与应用程序的SWF一起放置。这将允许您将托管在任何域上的SWF重新指向其他任何位置托管的后端。如果您在本地开发并连接到共享开发服务器,这将特别有用。

Compiling this information into your SWF is very inflexible and a poor practice.

将此信息编译到您的SWF中是非常不灵活的,也是一种糟糕的做法。

#4


I typically will look at the url in the contentLoaderInfo object in either the Application (Flex -- http://livedocs.adobe.com/flex/201/langref/mx/core/Application.html#url) or root display object (Flash -- http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html#url). If the url begins with "file", you know you are in your development/IDE, if it's "http", it's being run in a browser. If you're just working in the browser, you might also pass a parameter to the object that has something like

我通常会在Application(Flex - http://livedocs.adobe.com/flex/201/langref/mx/core/Application.html#url)或根显示对象中查看contentLoaderInfo对象中的url( Flash - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html#url)。如果url以“file”开头,你知道你在开发/ IDE中,如果它是“http”,它就是在浏览器中运行。如果您只是在浏览器中工作,您也可以将参数传递给具有类似功能的对象

{
    url: $_SERVER['SERVER_NAME'];
}

and perform some init/startup method to switch based on the path the app is running under.

并执行一些init / startup方法,根据应用程序运行的路径进行切换。

#5


I had this very issue in an AIR app I am writing that hits a Rails app via WebORB.

我在写的AIR应用程序中遇到了这个问题,它通过WebORB点击Rails应用程序。

I just need to switch between http://localhost and http://fakeproductionurl.com depending on whether I was running in Flex Builder (via adl).

我只需要在http:// localhost和http://fakeproductionurl.com之间切换,具体取决于我是否在Flex Builder中运行(通过adl)。

This is what I ended up using:

这是我最终使用的:

if (NativeApplication.nativeApplication.publisherID != "") {
   return "http://fakeproductionurl.com";
}
else {
   return "http://localhost";
}

It doesn't give you the ability to switch between 3+ different environments, but it's a very easy way to toggle between development / production environments.

它不能让您在3种以上的不同环境之间切换,但它是在开发/生产环境之间切换的一种非常简单的方法。

#1


You can use namespaces and configure the current namespace (DEV/RELEASE) in your compiler options.

您可以使用命名空间并在编译器选项中配置当前命名空间(DEV / RELEASE)。

CONFIG::release
public function connect()
{
  //connect to release url
}

CONFIG::dev
public function connect()
{
   //connect to dev url
}

then define these options for the compiler:

然后为编译器定义这些选项:

-define=CONFIG::release,false
-define=CONFIG::dev,true

#2


I suggest either using a configuration file or changing your hosts file to point domains to localhost or dev servers on your development machine. With the latter option you always use your production URLs in code, but your dev machine will resolve those domains to your local machine because it checks the hosts file first.

我建议使用配置文件或更改hosts文件,将域指向开发机器上的localhost或dev服务器。使用后一种选项,您始终在代码中使用生产URL,但您的开发机器会将这些域解析为本地计算机,因为它首先检查hosts文件。

#3


The best approach here is to externalize this information into a config file - perhaps an XML file - that is loaded via a relative url. The config file might like look this:

这里最好的方法是将此信息外部化为配置文件(可能是XML文件),该文件通过相对URL加载。配置文件可能看起来像这样:

<config>
    <serviceEndpoint>http://www.mydomain.com/services</serviceEndpoint>
</config>

Be sure to name your XML elements with valid ActionScript variable names or you may encounter some difficulty working with the file (for instance, E4X expressions may become difficult.

确保使用有效的ActionScript变量名称命名XML元素,否则您可能会遇到使用该文件的一些困难(例如,E4X表达式可能会变得困难。

You can then use HTTPService to load "config.xml" which is placed alongside your application's SWF when deployed. This will allow you to repoint a SWF hosted on any domain to backend hosted anywhere else. This is especially useful if you are developing locally and are connecting to a shared development server.

然后,您可以使用HTTPService加载“config.xml”,它在部署时与应用程序的SWF一起放置。这将允许您将托管在任何域上的SWF重新指向其他任何位置托管的后端。如果您在本地开发并连接到共享开发服务器,这将特别有用。

Compiling this information into your SWF is very inflexible and a poor practice.

将此信息编译到您的SWF中是非常不灵活的,也是一种糟糕的做法。

#4


I typically will look at the url in the contentLoaderInfo object in either the Application (Flex -- http://livedocs.adobe.com/flex/201/langref/mx/core/Application.html#url) or root display object (Flash -- http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html#url). If the url begins with "file", you know you are in your development/IDE, if it's "http", it's being run in a browser. If you're just working in the browser, you might also pass a parameter to the object that has something like

我通常会在Application(Flex - http://livedocs.adobe.com/flex/201/langref/mx/core/Application.html#url)或根显示对象中查看contentLoaderInfo对象中的url( Flash - http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/LoaderInfo.html#url)。如果url以“file”开头,你知道你在开发/ IDE中,如果它是“http”,它就是在浏览器中运行。如果您只是在浏览器中工作,您也可以将参数传递给具有类似功能的对象

{
    url: $_SERVER['SERVER_NAME'];
}

and perform some init/startup method to switch based on the path the app is running under.

并执行一些init / startup方法,根据应用程序运行的路径进行切换。

#5


I had this very issue in an AIR app I am writing that hits a Rails app via WebORB.

我在写的AIR应用程序中遇到了这个问题,它通过WebORB点击Rails应用程序。

I just need to switch between http://localhost and http://fakeproductionurl.com depending on whether I was running in Flex Builder (via adl).

我只需要在http:// localhost和http://fakeproductionurl.com之间切换,具体取决于我是否在Flex Builder中运行(通过adl)。

This is what I ended up using:

这是我最终使用的:

if (NativeApplication.nativeApplication.publisherID != "") {
   return "http://fakeproductionurl.com";
}
else {
   return "http://localhost";
}

It doesn't give you the ability to switch between 3+ different environments, but it's a very easy way to toggle between development / production environments.

它不能让您在3种以上的不同环境之间切换,但它是在开发/生产环境之间切换的一种非常简单的方法。