首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个独立的观测量角2上的两个顺序订阅

两个独立的观测量角2上的两个顺序订阅
EN

Stack Overflow用户
提问于 2016-09-23 21:59:41
回答 1查看 393关注 0票数 0

对于角2中的两个独立的可观测数据,我有两个顺序订阅的问题。我试图:

  1. 从坐标中获取位置
  2. 把这个位置附加到我的json上
  3. 将json发送到服务器

我这样做是我认为错误的:

代码语言:javascript
复制
this._locationService.geocode(this.location.latitude, this.location.longitude).
        subscribe(position => {
            this.location.city = this.findAddressPart(position, "locality", "long");
            this.location.country = this.findAddressPart(position, "country", "long");
            this._locationService.updateLocation(this.location)
                .subscribe(
                    location => {
                        this.location = location;
                        this.submitted = true;
                        this.submitting = false;
                    }
                );
        });

这样,在实际获取位置之后,我的DOM只更新5-10秒。

EN

回答 1

Stack Overflow用户

发布于 2016-09-24 11:23:49

似乎您有一个问题,您的解决方案需要多长时间来更新。不幸的是,除非您重构_locationService使用数据的方式,否则这是无法避免的。目前,你有:

  1. 从纬度和经度获取地理代码
  2. 等待请求完成
  3. 将数据从请求#1设置为位置
  4. 从位置获取更新数据
  5. 等待请求完成
  6. 设置更新位置
  7. 更新DOM w/更新位置

有两个请求链接在一起。如果可能的话,我会将这两个函数合并为后端上的一个调用,这样您就可以调用类似的

代码语言:javascript
复制
this._locationService.geocode(this.location.latitude, this.location.longitude).
        subscribe(location => {
            this.location = location;
            this.submitted = true;
            this.submitting = false;
        });

当然,只有当服务器包含用于服务此类请求的数据时,这才能起作用。如果您的服务器还必须进行HTTP调用,那么将其更改为上面的内容将是毫无意义的。

如果以上不可能,您可以在第一个请求完成后更新DOM。如果一切顺利,updateLocation函数将返回发送到服务器的相同位置,对吗?您可以用本地可用的值更新DOM,而不是在第二个函数成功时更新DOM,只有在出现错误时才进行更改。这将使您的响应时间看起来快了50%。就像这样。

代码语言:javascript
复制
this._locationService.geocode(this.location.latitude, this.location.longitude).
        subscribe(position => {
            this.location.city = this.findAddressPart(position, "locality", "long");
            this.location.country = this.findAddressPart(position, "country", "long");
            // SET DOM HERE using this.location values
            this._locationService.updateLocation(this.location)
                .subscribe(
                    location => {
                        this.location = location;
                        // optionally SET DOM HERE again
                        this.submitted = true;
                        this.submitting = false;
                    }, 
                    error => {
                        // SET DOM HERE reflecting error
                    }
                );
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39670146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档