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


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

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

         在Enyo中,通过enyo.Ajax 或enyo.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方法都被调用。

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

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

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

     1 // Get a new transaction; it's been started, but we can add more handlers
     2 // synchronously.
     3 var x = transaction();
     4 
     5 // Add a handler that will be called if an error is detected. This handler
     6 // recovers and sends a custom message.
     7 x.error(function(inSender, inResponse) {
     8     console.log("error: calling recover", inResponse);
     9     this.recover();
    10     return "recovered message";
    11 });
    12 
    13 // Add a response handler that halts response handler and triggers the
    14 // error chain. The error will be sent to the error handler registered
    15 // above, which will restart the handler chain.
    16 x.response(function(inSender, inResponse) {
    17     console.log("response: calling fail");
    18     this.fail(inResponse);
    19 });
    20 
    21 // Recovered message will end up here.
    22 x.response(function(inSender, inResponse) {
    23     console.log("response: ", inResponse);
    24 });

    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的例子:

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

    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的实例。返回的数据包含在onResponse或onError事件对象的data参数中。

    enyo.Ajax vs. enyo.WebService

         你可能已经注意到,enyo.WebService和enyo.Ajax在功能上有很多重叠。一般,我们建议使用enyo.Ajax,因为它不用声明为component组件。

  • 相关阅读:
    WPF 窗口图标与背景
    WPF Convert和RelativeSource
    属性更改通知(INotifyPropertyChanged)——针对ObservableCollection
    WPF 输入异常提示
    WPF 异常处理
    WPF DataGrid 触发器
    拉格朗日乘子法(Lagrange multiplier)和KKT条件
    在同一台电脑上添加多个ssh key
    重积分的换元法
    Matlab之显示输出
  • 原文地址:https://www.cnblogs.com/waimai/p/2853744.html
Copyright © 2020-2023  润新知