您的位置:首页 > 其它

enyo官方开发入门教程翻译一Consuming Web Services

2013-01-10 19:49 666 查看
从本篇开始,不再按Enyo官方教程的顺序翻译,先翻译一些我还没有弄明白的部分---与服务器通信、触摸事件和与phonegap的集成。官方教程的顺序是布局方法、通用UI的Control类的使用、主题、国际化、debug方法,然后才是现在翻译的这部分。

在本文中,我们将看到基于Enyo框架的app如何使用传统的web通信方法和在特定情况下的WebService通信方法。

在Enyo中,通过enyo.Ajaxenyo.WebService 发送Web请求。enyo.Ajax直接继承自enyo.Async---处理异步操作的基类。enyo.WebService使用enyo.Ajax或enyo.JsonpRequest(enyo.Async的另一个子类)来完成HTTP通信。

我们首先介绍核心的enyo.Async.

enyo.Async

再次重申,enyo.Async是处理异步通信的基类。

enyo.Async是一个Object而不是一个Component,所以你不能在components语句块中声名Async。如果你想把Async作为component组件来使用,那你应该使用enyo.WebService来代替。

Async对象代表一个未完成的task。你可以为Async指定回调函数,当该task完成或抛出错误时就会调用该函数。

要使用Async需要创建一个enyo.Async或它的子类的实例,然后再response或error方法中指定回调函数。

调用go()方法来开始异步操作。

处理程序可以是signature
(asyncObject, value)方法或enyo.Async的实例或它的子类实例。这允许异步对象的链接。


如果response方法返回一个值,这个值代替原值会发送到链中随后的处理程序。failure方法可以撤销错误的状态并切换到response方法。go()方法的默认实现方式会使所有response方法都被调用。

下面的示例演示了前面讲到的许多特性:

var transaction = function() {
// Create a transaction object.
var async = new enyo.Async();
// Cause handlers to fire asynchronously (sometime after we yield this thread).
// "initial response" will be sent to handlers as inResponse
async.go("intial response");
// Until we yield the thread, we can continue to add handlers.
async.response(function(inSender, inResponse) {
console.log("first response: returning a string,",
"subsequent handlers receive this value for 'inResponse'");
return "some response";
});
return async;
};


使用
transaction方法可以为Async对象添加处理方法,直到所有的方法都返回(同步):


// Get a new transaction; it's been started, but we can add more handlers
// synchronously.
var x = transaction();

// Add a handler that will be called if an error is detected. This handler
// recovers and sends a custom message.
x.error(function(inSender, inResponse) {
console.log("error: calling recover", inResponse);
this.recover();
return "recovered message";
});

// Add a response handler that halts response handler and triggers the
// error chain. The error will be sent to the error handler registered
// above, which will restart the handler chain.
x.response(function(inSender, inResponse) {
console.log("response: calling fail");
this.fail(inResponse);
});

// Recovered message will end up here.
x.response(function(inSender, inResponse) {
console.log("response: ", inResponse);
});


enyo.Ajax

enyo.Ajax继承了enyo.Async,对JS的XmlHttpRequest API进行了封装。

enyo.Ajax公布了 enyo.AjaxProperties对象的所有属性。你可以为这些属性设置不同的值来定制自己的HTTP 请求,如url、回调方法、请求头等。

和enyo.Async一样,enyo.Ajax也是一个Object不是Component,不要再components块中声明enyo.Ajax对象。如想把它作为component使用,请选择WebService来代替(WebService内部默认使用enyo.Ajax来完成HTTP通信)。

下面是一个使用enyo.Ajax从Yahoo取回唯一id的例子:

getWoeid: function(inPlace) {
// set up enyo.AjaxProperties by sending them to the enyo.Ajax constructor
var x = new enyo.Ajax({url: "http://query.yahooapis.com/v1/public/yql?format=json"});
// send parameters the remote service using the 'go()' method
x.go({
q: 'select woeid from geo.placefinder where text="' + inPlace + '"'
});
// attach responders to the transaction object
x.response(this, function(inSender, inResponse) {
// extra information from response object
var woeid = inResponse.data.query.results.Result.woeid;
// do something with it
this.setWoeid(inPlace, woeid);
});
}


enyo.JsonpRequest

enyo.JsonpRequest是使用JSONP请求远程服务器的一种特殊形式的enyo.Async。与普通的XmlHttpRequest不同,JsonP加载的是script。enyo.JsonpRequest经常用来跨域请求数据。你在使用WebService时将实例的jsonp设置为true就可以直接使用JSONP了,这时WebService内部使用enyo.JsonpRequest进行HTTP通信。

enyo.WebService

enyo.WebService是执行XHR 请求的组件,它是Async和子类enyo.Ajax、enyo.JsonpRequest的包装类。它默认使用enyo.Ajax,并且和enyo.Ajax一样公布了enyo.AjaxProperties对象的所有属性。你可以给
WebService实例设置不同的属性值来定制自己的HTTP请求。若要使WebService实例使用enyo.JsonpRequest,在实例中设置"jsonp: true"即可。


使用send方法发送请求并返回Async的实例。返回的数据包含在[code]onResponse或
onError事件对象的data参数中。


enyo.Ajax vs. enyo.WebService

你可能已经注意到,enyo.WebService和enyo.Ajax在功能上有很多重叠。一般,我们建议使用enyo.Ajax,因为它不用声明为component组件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: