我需要开发一个基于Facebook API的非常复杂的Flash网站,如果我有一种方法可以在本地开发而不必一直上传它,我将永远感激。
我看到一篇文章提到设置一些东西给localhost,但他们从未指明究竟是什么(is it possible to use facebook API locally?)
非常感谢。
答案 0 :(得分:1)
答案 1 :(得分:1)
在这种情况下,封装是你的朋友。当我使用外部/第三方API时,我喜欢为数据创建自己的包装类。假设你只关心'fbID'和'userName'。创建一个自己的类来保存这些数据(具有getter的私有变量,以及一个或多个setter)。一些骨架代码:
class MyUserClass{
//declare vars here (_fbID, _userName)
public function setData(userID:String, userName:String):void{
//set the values here.
}
//getters here (get fbID, get userName)
}
如果您愿意,可以使用2个setter功能,但重点是您可以使用所需的任何数据调用它们。当您的整个应用程序从您的类中获取此信息,而不是直接从api获取此信息时,您可以脱机工作。在离线模式下,您可以插入一些兼容的“假”数据以使其正常工作。
现在你需要通过为你对facebook发出的每个电话制作一个包装类型,将其提升到一个新的水平。我的意思是,既然你知道fb会发生什么,你可以假装你真的得到了它,并从那里开始。要求朋友ID列表?制作一个合理的假名单,让你的应用程序使用它。更好的是,根据需要生成尽可能多的虚假脱机用户,并在将假数据返回给事件监听器之前使服务器调用延迟一个随机的“滞后”时间。这也有助于测试可能的竞争条件。
实现此目的的一种方法是创建和扩展一个类来执行api调用。享受。
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
class MyApiCaller extends EventDispatcher{
//set up vars to hold call result data
protected var _someData:String;
//make sure to declare some event types for the callbacks
public static const SERVERCALL1_COMPLETE:String = "servercall1_complete";
function MyApiCaller(){
//init things....
}
public function doServerCall1(...args:*):void {
//create the ulrLoader etc...
//set up event listener to onServerCall1Complete
}
public function onServerCall1Complete(event:Event):void {
//parse results, save to vars
//fire event to notify the caller
dispatchEvent(new Event(SERVERCALL1_COMPLETE));
}
//getter to be used when the waiting object gets the SERVERCALL1_COMPLETE event
public function get someData():String {return _someData;}
}
class MyFakeApiCaller extends MyApiCaller{
//set up any additional types (random user data etc..) that would not be found in the base class
//no need to redeclare the event types
function MyFakeApiCaller(){
//init things....
}
override public function doServerCall1(...args:*):void {
//wait a random amount of time via Timer, set up event listener to onServerCall1Complete
}
override public function onServerCall1Complete(event:Event):void {
//event is a TimerEvent in this case
//generate data / choose random data
//save to vars: _someData = ...
//fire event to notify the caller
dispatchEvent(new Event(MyApiCaller.SERVERCALL1_COMPLETE));
}
//getter from base class will be used as usual
}