ui5详细教程Stone

SAPUI5 (09) - 模块化

2017-01-03  本文已影响493人  Stone0823

OpenUI5 的模块化

JavaScript 并没有对模块提供内在支持,下一代版本 (ECMAScript 2015) 将提供,ECMAScript 2015 可能还需要相当长的一段时间才能普及。目前第三方的解决方案主要有两种:

而 OpenUI5 对 module(模块)提供内在的支持。在 OpenUI5 中,模块指的是可以在浏览器 (browser) 中加载和执行的 JavaScript 文件。如何将代码组织成不同的模块,并没有一个统一的规则,取决于开发人员的想法。当然,一般来说,同一个模块的内容应该有着共同的主题,而不是随意将代码分割成不同的文件。

如何加载模块

jQuery.sap.declare

语法:

jQuery.sap.declare(sModuleName, bCreateNamespace?) : void

说明:声明一个模块,以确保模块存在。这个语句必须出现在模块代码(也就是代码文件)的第一句。官方文档已标记为从 v1.52 版本后过时 (depreciated)。

示例一示例代码来源)

// A module declaration, ensures that sap.ui.sample exists
jQuery.sap.declare("sap.ui.sample.MyClass");

// now it is safe to use that namespace object and assign a new member 'MyClass'
// to it
// Note that jQuery.sap.declare does not create the MyClass object.
// So the developer can decide whether it should be a function, an object,
// or an instance of a specific type
sap.ui.sample.MyClass = { key1 : 'value1' };

// the following line guarantees that <code>sap.ui.sample.subspace</code>
// is a valid object
sap.ui.namespace("sap.ui.sample.subspace");

// now one can use this namespace as well
sap.ui.sample.subspace.member1 = 42;
sap.ui.sample.subspace.member2 = 3.141;

示例二

本示例代码说明如何使用jQuery.sap.declarejQuery.sap.require 创建 controller 模块。

    jQuery.sap.declare("example.MyController");
    jQuery.sap.require("sap.ui.core.mvc.Controller");

    "use strict";

    sap.ui.core.mvc.Controller.extend("example.MyController", {
        onInit: function () {
        }
   });

jQuery.sap.require

语法:

 jQuery.sap.require(vModuleName)

确保当前代码继续之前,所指定的模块被加载和执行。如果所需要的模块没有被加载,将会被同步加载和执行,如果已经加载,就忽略。原文

Ensures that the given module is loaded and executed before execution of the current script continues.
By issuing a call to this method, the caller declares a dependency to the listed modules.
Any required and not yet loaded script will be loaded and execute synchronously. Already loaded modules will be skipped.

sap.ui.define()

语法:

sap.ui.define(sModuleName?, aDependencies?, vFactory, bExport?)

定义一个 module,并指定其依赖的模块,所定义的 module 具有全局命名空间 (global namespace)。global namespace 的意思是 module 中的对象在整个 Application 中可见。

函数有四个参数,一般只需要参数 aDependencies 和参数 vFactory

sap.ui.define() 函数被广泛用于定义 controller。一般建议省略参数 sModuleName,也就是说不要对模块名称进行硬编码,这样对模块的访问较方便。

sap.ui.require()

用于解决 (resolve) 代码的依赖模块。和 jQuery.sap.require() 不同,sap.ui.require() 是异步加载。

使用 sap.ui.define() 定义 controller

刚才介绍了 sap.ui.define() 的语法,下面介绍如何使用 sap.ui.define() 定义controller。我们以上一篇的代码为例,对 controller 的代码进行改写。

改写之前的代码:

sap.ui.controller("suppliers.supplieroverview", {
    onInit: function() {        
        var oModel = sap.ui.model.json.JSONModel('models/suppliers.json');          
        this.getView().setModel(oModel);
    }
}); 

改写之后的代码:

sap.ui.define(["sap/ui/core/mvc/Controller"], 
    function(Controller){
        "use strict";
        
        return Controller.extend("suppliers.supplieroverview", {
            onInit: function() {
                var oModel = sap.ui.model.json.JSONModel();
                oModel.loadData('models/suppliers.json');
                this.getView().setModel(oModel);
            }
        });
    }
);

这个代码可以看作模式化固定固定代码,我们一般只需要基于这个代码片段作三个方面的变更:

上一篇 下一篇

猜你喜欢

热点阅读