我目前正在研究消耗OData服务的SAP Fiori app。
我在我的控制器fonct
中创建了一个方法来计算来自我的OData的变量。
我想捕获这个值,并在每次刷新视图时将其放在一个全局变量中。
我创建了一个像这样的全局变量:
var boo1;
return Controller.extend("com.controller.Detail", {...});
我在boo1
方法中使用fonct
作为参数在我的方法onInit
中,但它是undefined
。
这是我控制器代码的片段:
sap.ui.define([
"com/util/Controller"
], function(Controller) {
"use strict";
var boo1;
return Controller.extend("com.controller.Detail", {
onInit: function() {
this.fonct(boo1);
alert(boo1);
},
fonct: function(ovar) {
var that = this;
var oModel = that.getView().getModel();
oModel.read("/alertSet", {
success: function(data) {
var a = JSON.stringify(data);
var b = a.slice(332,-4);
ovar = b;
}
});
}
});
});
您应该在全局范围内声明全局变量(即,在sap.ui.define
之前)。
请避免定义全局变量(例如在sap.ui.define
之前),特别是如果您正在处理将要放入Fiori Launchpad的应用程序。这种方法是considered harmful。文档中也重复了这一点:
在控件和类模块中,不应在all.(src)中使用全局变量
我们故意使用"use strict"
来禁止这种做法。
此外,在sap.ui.getCore()
上定义属性或添加模型是Fiori应用程序的反模式。这不仅会导致与定义全局变量相同的缺陷,而且默认情况下会在核心are not propagated to the views上设置模型,因为组件意味着模块化使用。因此,将这些数据定义到相应的组件而不是核心是Fiori应用程序的方法。
我创建了一个全局变量
var boo1; return Controller.extend("com.controller.Detail", {...});
你创建的不是全局变量,因为boo1
是在匿名函数中声明的。相反,boo1
存储在closure中,允许原型方法(onInit
和fonc
)维持词汇环境,其中boo1
不仅可以通过方法获得,而且可以通过其他com.controller.Detail
实例访问。就Java而言,我们可以说boo1
是一个私有静态变量。但它不是一个全球变量。
话虽如此..
我通过
boo1
作为参数在我的方法fonct
里面我的onInit
方法,但它是undefined
封闭使得传递boo1
作为参数是不必要的(除非你想要boo1
的硬拷贝来维持它的多种状态)。方法fonct
可以直接访问boo1
,即使在fonct
中定义的匿名回调中也是如此。
现在,你可能会问为什么boo1
是undefined
。有两个原因:
boo1
之前,undefined
是fonct
。如果boo1
是一个对象而不是未定义的,那么ovar
将是对boo1
对象的引用,并且任何ovar
属性的更改都将发生在boo1
对象中。但undefined
不是一个对象。所以ovar
根本不知道boo1
是什么。注意:JS的评估策略是“Call by object-sharing”。alert(boo1)
在打电话给fonct
后被解雇了。除非oModel
同步工作(我强烈建议不要这样做),浏览器不会等待成功回调被触发以便稍后调用alert(boo1)
。警报立即触发,然后成功回调boo1
应该被操纵。删除ovar
并使用boo1
代替在成功回调中正确更新boo1
。
fonct: function(/*no ovar*/) {
//...
oModel.read("/alertSet", {
success: function(data) {
//...
boo1 = /*new value*/;
alert(boo1);
}.bind(this)
});
}
我认为你想做的事情比你正在做的更简单。要保存全局变量,请获取Core对象并将该变量设置为此对象的新属性:
sap.ui.getCore()。myGlobalVar = myCalculatedValue;
然后在其他视图中使用它,直接从Core获取属性:
var mySavedVar = sap.ui.getCore()。myGlobalVar
然后使用Router routeMatched事件来处理导航并刷新值。
这里有一个片段:https://jsbin.com/bewigusopo/edit?html,output
<!DOCTYPE html>
<html><head>
<meta http-equiv='X-UA-Compatible' content='IE=edge' >
<meta charset="UTF-8" >
<title>test</title>
<script id='sap-ui-bootstrap'
src='https://sapui5.hana.ondemand.com/1.38.5/resources/sap-ui-core.js'
data-sap-ui-theme='sap_bluecrystal'
data-sap-ui-bindingSyntax="complex"></script>
<script id="view1" type="sapui5/xmlview">
<mvc:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc">
<core:ComponentContainer name='my.comp'/>
</mvc:View>
</script>
<script id="home" type="sapui5/xmlview">
<mvc:View xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
controllerName="my.controller1">
<Page>
<Input id="input" placeholder="Write a text to save it globally"/>
<Button text="Navigate to other view" press="onNavigate"/>
</Page>
</mvc:View>
</script>
<script id="add" type="sapui5/xmlview">
<mvc:View xmlns="sap.m" xmlns:f="sap.ui.layout.form" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
controllerName="my.controller2">
<Page id="page" showNavButton="true" navButtonPress="onBack">
<HBox class="sapUiLargeMarginBegin">
<Label text="The global variable is:" class="sapUiSmallMarginEnd sapUiSmallMarginTop"/>
<Input id="inputResult"/>
</HBox>
</Page>
</mvc:View>
</script>
<script>
// jQuery.sap.declare("my.comp.Component");
sap.ui.define("my/comp/Component", ["sap/ui/core/UIComponent"], function(UIComponent) {
return UIComponent.extend("my.comp.Component", {
metadata : {
name : "GreatComponent",
version : "1.0",
includes : [],
dependencies : {
libs : ["sap.m"]
},
routing: {
config: {
routerClass: "sap.m.routing.Router",
viewType: "XML",
viewPath: "my",
controlId: "app",
transition: "slide",
controlAggregation: "pages"
},
routes: [
{
name: "home",
pattern: "",
target: "home"
},
{
name: "add",
pattern: "add",
target: "add"
}
],
targets: {
home: {
viewName: "Home",
title: "home"
},
add: {
viewName: "Add",
title: "add"
}
}
}
},
init: function() {
sap.ui.core.UIComponent.prototype.init.apply(this, arguments);
var oRouter = this.getRouter();
var oViews = oRouter.getViews();
this.runAsOwner(function() {
var myHome = sap.ui.xmlview({viewContent:jQuery('#home').html()});
oViews.setView("my.Home", myHome);
var myAdd = sap.ui.xmlview({viewContent:jQuery('#add').html()});
oViews.setView("my.Add", myAdd);
});
oRouter.initialize();
},
createContent : function() {
var componentData = this.getComponentData();
return new sap.m.App("app", {
});
}
});
});
sap.ui.define("my/controller1", [
"sap/ui/core/UIComponent"
],function(UIComponent) {
return sap.ui.controller("my.controller1", {
onInit: function() {
this.oRouter = UIComponent.getRouterFor(this.getView());
},
onNavigate: function() {
var sInputText = this.getView().byId("input").getValue();
sap.ui.getCore().myGlobalVar = sInputText;
console.log(sap.ui.getCore().myGlobalVar)
this.oRouter.navTo("add");
}
});
});
sap.ui.define("my/controller2", [
"sap/ui/core/UIComponent"
],function(UIComponent) {
return sap.ui.controller("my.controller2", {
onInit: function() {
this.oRouter = UIComponent.getRouterFor(this.getView());
this.oRouter.getRoute("add").attachPatternMatched(this._onObjectMatched, this);
},
_onObjectMatched: function(){
var sGlobalVariable = sap.ui.getCore().myGlobalVar;
console.log(sGlobalVariable);
this.getView().byId("inputResult").setValue(sGlobalVariable);
},
onBack: function(){
this.oRouter.navTo("home");
}
});
});
sap.ui.require(["my/comp/Component"], function(myComp) {
// instantiate the View
sap.ui.xmlview({viewContent:jQuery('#view1').html()}).placeAt('content');
});
</script>
</head>
<body class='sapUiBody'>
<div id='content'></div>
</body>
</html>
其他可能性是设置一个全局模型,它将简单地绑定你的绑定。只需创建它并将其设置为Core
//To set it
var oGlobalModel = new sap.ui.model.json.JSONModel();
sap.ui.getCore().setModel(oGlobalModel, "myGlobalModelID");
//To get it
var oMyGlobalModel = sap.ui.getCore().getModel("myGlobalModelID");