SCORM API 中的异步 Ajax 调用
我正在为 SCORM 2004 第 4 版创建一个 javascript API。 对于那些不了解 SCORM 的人来说,它基本上是一个 API 标准,电子学习课程可以使用它与 LMS(学习管理系统)进行通信。 现在 API 必须具有以下方法:
- Initialize(args)
- GetValue(key)
- SetValue(key, value)
- Terminate(args)
- Commit(args)
- GetDiagnostic(args)
- GetErrorString(args)
- GetLastError()
现在必须在调用之前调用 Initialize其他任何事情,终止必须是最后一个。 GetValue/SetValue 可以在两者之间的任何位置调用。 我正在做的是在 Initialize 方法中,我从 Web 服务获取一些 JSON 并将其存储在 API 中(稍后使用 GetValue/SetValue 方法时使用)。 我遇到的问题是通过 jQuery 的 AJAX 调用是异步的,因此可以在加载 JSON 之前完成 Initialize 方法调用。 既然如此,在调用 Initialize 之后调用 GetValue 可能会导致意外问题,因为 GetValue 使用的 JSON 尚不存在。 我的问题是:如何确保在调用 GetValue/SetValue 方法之前加载 JSON? 我知道简单的答案是使其同步,但大多数情况下不建议这样做,而且它似乎不想为我这样做。 这是我的代码:
function GetJSON(){
var success = false;
$.ajaxSetup({async:false}); //should make it synchronous
$.getJSON("http://www.mydomain.com/webservices/scorm.asmx/SCORMInitialize?
learnerID=34&jsoncallback=?",
function(data){
bind(data);
success = true;
}
);
return success;
}
function bind(data){
this.cmi = eval("(" + data.d + ")");
$.ajaxSetup({async:true}); //should make it asynchronous again
}
有人有什么想法吗? 我真的很感激!
I am creating a javascript API for SCORM 2004 4th Edition. For those who don't know about SCORM, basically it is an API standard that eLearning courses can use to communicate with an LMS (Learning Management System). Now the API has to have the following method:
- Initialize(args)
- GetValue(key)
- SetValue(key, value)
- Terminate(args)
- Commit(args)
- GetDiagnostic(args)
- GetErrorString(args)
- GetLastError()
Now Initialize has to be called before anything else, and Terminate must the last. GetValue/SetValue can be called anywhere in between there. What I am doing is in the Initialize method I am getting some JSON from a web service and storing that in the API (to be used when using the GetValue/SetValue methods later). The problem I am coming across is that the AJAX call via jQuery is asynchronous, so the Initialize method call could be done before the JSON is loaded. With that being the way it is, a call to GetValue after calling Initialize could cause unexpected issues b/c the JSON that GetValue uses isn't there yet. My question is this: What can I do to ensure that the JSON is loaded before the GetValue/SetValue methods are called? I know the simple answer is to make it synchronous, but that is not advised mostly, and it doesn't seem to want to do that for me. Here is my code regarding that:
function GetJSON(){
var success = false;
$.ajaxSetup({async:false}); //should make it synchronous
$.getJSON("http://www.mydomain.com/webservices/scorm.asmx/SCORMInitialize?
learnerID=34&jsoncallback=?",
function(data){
bind(data);
success = true;
}
);
return success;
}
function bind(data){
this.cmi = eval("(" + data.d + ")");
$.ajaxSetup({async:true}); //should make it asynchronous again
}
Does anyone have any ideas? I would really appreciate it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你已经很好地阐述了问题。 SCO 调用Initialize 后,CMI 数据需要立即可供SCO 进行后续的GetValue 调用。 但是,不建议进行同步 AJAX 调用,如果请求中出现挂起,它可能会锁定整个浏览器,直到请求返回或超时。 解决方案是在加载 SCO 之前预加载所有必需的数据。 在我们的 SCORM 引擎实现中,我们预加载了所有播放器启动时收集数据(CMI 和排序),然后在学习者学习课程的过程中使用后台进程定期提交脏数据。 在处理可能的窗口启动和退出场景的组合时,确保正确保留所有数据可能会有点棘手,但这当然是可能的。 您需要避免从 SCORM API 调用中向服务器发出任何请求,因为 SCO 经常会用大批量的调用淹没 LMS。 在这些调用中发出服务器请求会严重降低学习者的体验,并给服务器带来性能负担。
麦克风
You've articulated the problem well. After the SCO calls Initialize, the CMI data needs to be immediately available for the SCO to make subsequent GetValue calls. However, making synchronous AJAX calls isn't advised, if there is a hangup in the request, it can lock up the entire browser until the request returns or times out. The solution is to pre-load all of the required data before the SCO is loaded. In our SCORM Engine implementation, we preload all of the data (CMI and sequencing) when the player is launched and then use a background process to periodically commit dirty data as the learner progresses through the course. It can get a bit tricky to ensure that all data is properly persisted when dealing with the combinations of possible window launching and exit scenarios, but it's certainly possible. You will want to avoid any requests to the server from within a SCORM API call as SCOs will often flood the LMS with big batches of calls. Making server requests within those calls can seriously degrade the learner's experience and place a performance burden on the server.
Mike
我们解决这个问题的方法是在 SCO 启动时将 CMI 数据排队到 API 中。 我们首先导航到启动页面,将 CMI 数据加载到 API 队列中,然后启动页面实际启动 SCO。 当SCO调用initialize时,我们只需将数据移入CMI即可。
The way we approached this problem was to queue the CMI data in the API when the SCO is launched. We first navigate to a launch page that loads the CMI data into the API's queue, and then the laucnch page actually launches the SCO. When the SCO calls intialize, we just move the data into the CMI.