javascript 在 SAPUI5 中实例化片段时传递另一个控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27095330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass another controller when instantiating a fragment in SAPUI5
提问by njames
In the SAPUI5 / OpenUI5 xmlfragment documentationthe third parameter is a controller for handling actions from the fragment.
在 SAPUI5 / OpenUI5 xmlfragment 文档中,第三个参数是用于处理来自片段的操作的控制器。
This is critical for a dialog fragment where there are buttons to press etc.
这对于有按钮可按下等的对话框片段至关重要。
Most of the time I have seen this instantiated as this
or sap.ui.getCore().byId('<element>').getController())
大多数时候我看到这个实例化为this
或sap.ui.getCore().byId('<element>').getController())
See an example at Fragment not get correct Controller
请参阅Fragment 中的示例未获得正确的控制器
Because of the complexity in a particular dialog I would like to have a separate controller for it.
由于特定对话框的复杂性,我想为它设置一个单独的控制器。
I have looked around at this and had a few attempts but so far not successful.
我环顾四周并进行了一些尝试,但到目前为止还没有成功。
I have put a working example on githubof using this
.
我在github上放了一个使用this
.
But I would like to instantiate Dialog.js
as the controller for the Dialog.fragment.xml
from initial.view.controller
但我想实例Dialog.js
化为Dialog.fragment.xml
from的控制器initial.view.controller
Any takers?
有接班人吗?
Pull requests gladly received.
很高兴收到拉取请求。
The Crux of the example is as follows (this is the initial.controller.js) :
例子的关键如下(这是initial.controller.js):
sap.ui.controller("sc.test.view.initial", {
oDialog: null,
openTestDialog: function(){
console.log("in open dialog");
// instantiate the other controller
var oDialogController = new sc.test.view.Dialog();
// this next commented line is the 'normal' way to do it
// oDialog = new sap.ui.xmlfragment( "sc.test.view.Dialog", this); //oDialogController);
// this is what I would like to achieve
oDialog = new sap.ui.xmlfragment( "sc.test.view.Dialog", oDialogController);
oDialog.open();
},
onCartDialogCancel:function(oEvent){
// this function would then be in the other controller but how to get a handle on the dialog?
oDialog.close();
}
});
});
Thanks.
谢谢。
采纳答案by qmacro
回答by Jasper_07
only example i could find close to yours was in the Material Shortage Fiori app
我在 Material Shortage Fiori 应用程序中可以找到与您的相似的唯一示例
oCtrl = sap.ui.controller("myapp.fragments.DirectCallDialog");
oDirectCallDialog = sap.ui.xmlfragment("myapp.fragments.DirectCallDialog", oCtrl);
lots of examples of injecting a controller when the fragment was called from a helper class. The helper class promotes reuse, eg same dialog fragment can be called from multiple views/components. The helper class method for the dialog setup is called from within a controller and the oController parameter is set to 'this'.
当从辅助类调用片段时注入控制器的许多示例。helper 类促进重用,例如可以从多个视图/组件调用相同的对话框片段。对话框设置的助手类方法从控制器内调用,oController 参数设置为“this”。
hth jsp
第一个jsp
回答by KynanJ
I copied an existing controller.js, and renamed it.
我复制了一个现有的 controller.js,并重命名了它。
Then, instantiated that as a below, and passed it through with the fragment.
然后,将其实例化为下面的,并通过片段传递它。
var oNewController = new sap.ui.core.mvc.Controller("myProject.DialogController"); this._oDialog = sap.ui.xmlfragment("myPopup","myProject.fragments.myPopup", oNewController);
var oNewController = new sap.ui.core.mvc.Controller("myProject.DialogController"); this._oDialog = sap.ui.xmlfragment("myPopup","myProject.fragments.myPopup", oNewController);
All eventing is now handled in oNewController, rather than the previously used "this"...
所有事件现在都在 oNewController 中处理,而不是以前使用的“this”...