使用代理模式改善SAP UI5应用的图片加载体验

简介: 使用代理模式改善SAP UI5应用的图片加载体验

For training purpose I am already implemented samples of various variants of proxy design pattern implementation in ABAP and Java. And now I need to find a meaningful example for proxy pattern used in JavaScript.


Let’s review the concept of proxy pattern in Wikipedia:


“A proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.“

Let’s use a real case to illustrate its usage.


Normal Solution

I have an XML view which contains one Image control:

image.pngAnd implement it in my controller.

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller){
  "use strict";
  return Controller.extend("buttontutorial.view.simple",{
       BIG_IMAGE: "https://cloud.githubusercontent.com/assets/5669954/24836808/7d78976e-1d58-11e7-9c28-2c76d90c9e12.png",
       onPress: function(){
    var image = this.byId("jerryImage");
    this.loadImageNormal(image);
       },
       loadImageNormal: function(image){
    image.setSrc(this.BIG_IMAGE);
       }
       });
    }
);

Nothing special. The drawback of this solution is, when you try to load a big image file, only a fragment of image is displayed first and then accompanied with the left part gradually.


Proxy Solution

New source code of controller:


sap.ui.define(["sap/ui/

sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller){
  "use strict";
  return Controller.extend("buttontutorial.view.simple",{
     BIG_IMAGE: "https://cloud.githubusercontent.com/assets/5669954/24836808/7d78976e-1d58-11e7-9c28-2c76d90c9e12.png",
     onPress: function(){
    var image = this.byId("jerryImage");
    this.loadImageWithProxy(image);
     },
     injectProxy: (function(){
    var imgProxy = new Image();
                return function(img, src){
                    imgProxy.onload = function(){
            img.setSrc(this.src);
                    };
                    img.setSrc("buttontutorial/view/loading.gif");
                    imgProxy.src = this.BIG_IMAGE;
                };
     })(),
     loadImageWithProxy: function(image){
    this.injectProxy(image);
           }
     });
   }
);

In this solution, once button is pressed:


(1) a local animation gif will be displayed as loading indicator.

(2) At the same time, a proxy Image instance is created which issues the load of the big file in secret.

(3) When the loading of big file is finished, the onload callback is called, and only at this time the image control defined in xml view itself could display the completely loaded image file.



相关文章
|
1月前
|
前端开发 编解码 数据格式
浅谈响应式编程在企业级前端应用 UI 开发中的实践
浅谈响应式编程在企业级前端应用 UI 开发中的实践
24 0
浅谈响应式编程在企业级前端应用 UI 开发中的实践
|
2月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
32 0
|
2月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
17 0
|
2月前
|
Web App开发 数据采集 前端开发
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
21 0
|
13天前
|
前端开发 搜索推荐 UED
【Flutter前端技术开发专栏】Flutter中的高级UI组件应用
【4月更文挑战第30天】探索Flutter的高级UI组件,如`TabBar`、`Drawer`、`BottomSheet`,提升应用体验和美观度。使用高级组件能节省开发时间,提供内置交互逻辑和优秀视觉效果。示例代码展示了如何实现底部导航栏、侧边导航和底部弹出菜单。同时,自定义组件允许个性化设计和功能扩展,但也带来性能优化和维护挑战。参考Flutter官方文档和教程,深入学习并有效利用这些组件。
【Flutter前端技术开发专栏】Flutter中的高级UI组件应用
|
20天前
|
机器学习/深度学习 人工智能 自然语言处理
【AI大模型应用开发】3.2 RAG实战 - RAG应用+UI实现加载本地文件并对话
【AI大模型应用开发】3.2 RAG实战 - RAG应用+UI实现加载本地文件并对话
35 0
|
1月前
关于 SAP ABAP OData 服务如何实现 Deep Insert 场景 - SAP 应用的标准行为试读版
关于 SAP ABAP OData 服务如何实现 Deep Insert 场景 - SAP 应用的标准行为试读版
16 1
|
2月前
|
开发者 UED
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
关于 SAP UI5 sap.m.Column 的 demandPopin 属性
16 0
|
2月前
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
SAP UI5 Link 控件的使用方法介绍 - 后续学习 Fiori Elements Smart Link 的基础试读版
16 0
|
2月前
|
XML 存储 数据格式
SAP UI5 控件 customData 属性的应用介绍
SAP UI5 控件 customData 属性的应用介绍
35 0

热门文章

最新文章