Dialog is not a constructor ui5 - sapui5

I get an error while using an reusing a dialog in sapui5:
Dialog is not a constructor
I want to create a dialog fragment. dialog.js contains all functions and after that make it global in the component.js
sap.ui.define([
"sap/ui/base/Object"
], function (Object) {
"use strict";
return Object.extend("tmp.Zprojetyousra.controller.Dialog", {
constructor : function (oView) {
this._oView = oView;
},
open : function () {
var oView = this._oView;
var oDialog = oView.byId("dialog");
// create dialog lazily
if (!oDialog) {
var oFragmentController = {
onCloseDialog : function () {
oDialog.close();
}
};
// create dialog via fragment factory
oDialog = sap.ui.xmlfragment(oView.getId(), "tmp.Zprojetyousra.view.Dialog", oFragmentController);
// connect dialog to the root view of this component (models, lifecycle)
oView.addDependent(oDialog);
}
oDialog.open();
}
});
});
HTML:
<core:FragmentDefinition
xmlns="sap.m"
xmlns:core="sap.ui.core">
<Dialog
id="dialog"
title="Hello dialogs}">
<content>
<core:Icon
src="sap-icon://hello-world"
size="8rem"
class="sapUiMediumMargin"/>
</content>
<beginButton>
<Button
text="{i18n>dialogCloseButtonText}"
press="onCloseDialog"/>
</beginButton>
</Dialog>
</core:FragmentDefinition>
JS:
sap.ui.define([
"sap/ui/core/UIComponent",
/* "tmp/Zprojetyoussra/model/models", peut influencer sur le code */
"sap/ui/model/json/JSONModel",
"tmp/Zprojetyoussra/controller/Dialog" ,
"sap/ui/Device"
/// device toujours doit etre a la fin des dependecies
], function (UIComponent, JSONModel, Device, Dialog) {
"use strict";
return UIComponent.extend("tmp.Zprojetyoussra.Component", {
metadata: {
// rootView: "tmp.Zprojetyoussra.view.tesstview"
manifest: "json"
},
init: function () {
// call the base component's init function
UIComponent.prototype.init.apply(this, arguments);
// set data model
var oData = {
recipient : {
name : "World"
}
};
// without this.getview
var oModel = new sap.ui.model.json.JSONModel(oData);
this.setModel(oModel);
// set i18n model
var i18nModel = new sap.ui.model.resource.ResourceModel({
bundleName : "tmp.Zprojetyoussra.i18n.i18n"
});
this.setModel(i18nModel, "i18n");
/* eslint-disable no-alert */
// debug code to show an alert for missing destination or CORS issues in the tutorial (see step 26 for details)
this.getModel("invoice").attachEventOnce("metadataFailed", function(oEvent) {
alert("Request to the OData remote service failed.\nRead the Walkthrough Tutorial Step 26 to understand why you don't see any data here.");
});
// set device model
var oDeviceModel = new JSONModel(Device);
oDeviceModel.setDefaultBindingMode("OneWay");
this.setModel(oDeviceModel, "device");
this._dialog= new Dialog();
// create the views based on the url/hash
this.getRouter().initialize();
}
});
});
Error:

You have a wrong order of your imports in the controller define section. In the array you have:
UIComponent, JSONModel, Dialog, Device
but in the function definition below you have:
UIComponent, JSONModel, Device, Dialog
So the Dialog variable holds Device namespace which doesn't have a controller.

As #vasek said put your import in the right order use this syntax in your js file :
sap.ui.define([
"sap/ui/core/UIComponent",
"sap/ui/model/json/JSONModel",
"tmp/Zprojetyoussra/controller/Dialog" ,
"sap/ui/Device"
], function (UIComponent, JSONModel, Dialog, Device) {
"use strict";
return UIComponent.extend("tmp.Zprojetyoussra.Component", {
metadata: {
// rootView: "tmp.Zprojetyoussra.view.tesstview"
manifest: "json"
},

Related

how to set a variable that is shared between multiple controllers

Is it a good idea to set a js file(in util folder) which return a global object which will keep a list of different map of variables .
So in the index.js I can pass the app variable and followed by accessing it from controller.
sap.ui.define(["sap/ui/base/ManagedObject"], function (ManagedObject) {
"use strict";
let globals = {};
return globals;
});
and in index.js
sap.ui.define(
[
"sap/ui/base/ManagedObject",
"zap/util/globals",
],
function (ManagedObject, globals) {
"use strict";
let oApp = new sap.m.App();
globals.oApp = oApp;
});
and in controller event handler
sap.ui.define(
["sap/ui/core/mvc/Controller", "zap/util/globals"],
function (Controller, globals) {
"use strict";
return Controller.extend("zap.controller.Master", {
onListPress: function (oEvt) {
let oApp = globals.oApp;
}
});
});
You can create a model, and load it in the manifest.json, so it would be accessible from each part of your application.
{
"models": {
"globalModel": {
"type": "sap.ui.model.json.JSONModel",
"uri": "Models/GlobalModel.json"
}
}
}`

"Uncaught TypeError: sap.ui.require.toUrl is not a function"

An error is occurring while I am trying to learn Fiori. Any help will be appreciated.
In onInit, it says:
Uncaught TypeError: sap.ui.require.toUrl is not a function.
sap.ui.define([
"sap/ui/core/mvc/Controller",
// ...
], function(Controller /*...*/) {
"use strict";
return Controller.extend("Upload_TestUpload_Test.controller.View1", {
onInit: function() {
var sPath = sap.ui.require.toUrl("Upload_Test/uploadCollection.json");
this.getView().setModel(new JSONModel(sPath));
},
// ...
});
});
Sap UI Version : "1.52.12"
var sPath = sap.ui.require.toUrl("Upload_Test/uploadCollection.json");
Sap UI Version : "1.52.12"
The API sap.ui.require.toUrl is available only as of 1.58.0. If updating UI5 is not possible, you'll have to use the API jQuery.sap.getResourcePath instead:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"jquery.sap.global",
// ...
], function(Controller, jQuery /*...*/) {
"use strict";
return Controller.extend("...", {
onInit: function() {
var sPath = jQuery.sap.getResourcePath("Upload_Test/uploadCollection.json"); // Use sap.ui.require.toUrl from UI5 1.58. The jQuery API is deprecated!
this.getView().setModel(new JSONModel(sPath));
// ...
},
// ...
});
});
API reference: jQuery.sap.getResourcePath

Error while setting model inside onInit() of root view - no error / no log

I am trying to set a new JSONModel on the Main.view.xml (root view).
But it seems like it is stoping at .setModel(). The console.log("after") is not logging.
sap.ui.define([
"sap/ui/core/mvc/Controller",
"jquery.sap.global",
"sap/m/MessageToast",
"sap/ui/model/json/JSONModel"
], function (Controller, JSONModel, MessageToast) {
"use strict";
return Controller.extend("sap.ui.bookedTimes.wt.controller.Main", {
onInit : function () {
var jModel = this.getOwnerComponent().getModel("zCatsTestJ");
var that = this;
jModel.attachRequestCompleted(function() {
console.log(that.getView());
var oViewModel= new JSONModel({workdate: "test"});
console.log("before");
that.getView().setModel(oViewModel, "view");
console.log("after");
console.log(that.getView().getModel("view"));
});
},
});
});
Error in console
Entry in manifest.json:
"sap.ui5": {
"rootView" : {
"viewName":"sap.ui.bookedTimes.wt.view.Main",
"id": "mainview",
"type": "XML"
},
Is there a problem in onInit() of the root view?
Update:
I should have added the part of the xml.view. I changed the view name to "view1" and everything from the controller was logged. The problem was that my view was still expecting a date
<Text text="{ path: 'view1>/workdate', type: 'sap.ui.model.type.Date', formatOptions: { pattern: 'dd.MM.yyyy' } }" />
After changing this to text it was working. Anyway the initial problem was the order of the definitions
Thanks guys
It looks like your imports are off. Try fixing it like this (pay attention to the define([]) block)
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/json/JSONModel",
"sap/m/MessageToast"
], function (Controller, JSONModel, MessageToast) {
"use strict";
return Controller.extend("sap.ui.bookedTimes.wt.controller.Main", {
onInit : function () {
var jModel = this.getOwnerComponent().getModel("zCatsTestJ");
var that = this;
jModel.attachRequestCompleted(function() {
console.log(that.getView());
var oViewModel= new JSONModel({workdate: "test"});
console.log("before");
that.getView().setModel(oViewModel, "view");
console.log("after");
console.log(that.getView().getModel("view"));
});
},
});
});
Now you should have JSONModel correctly imported and shouldn't see any errors.

Sapui5: How can I add a button list to custom control?

I am getting data from oModel, and it {msgData} object
var Buttons = [{text:"apple"},{text:"banana"}];
var sQuery = "some text...";
oModel.oData.msgData.push({
Type : "Information",
buttons:Buttons,
customIcon:"media/chat/b_small.png",
Text: sQuery
});
oModel.refresh();
(in xml file, you can see the code below)
XML:
<wt:MessageStrip
text="{msgData>Text}"
type="{msgData>Type}"
>
// ***** NEED TO ADD THESE LINES ****
<List items="{msgData>buttons}" class="fixFlexFixedSize BtnBox">
<Button press="BtnClick" text="{msgData>text}" class="sapUiTinyMarginEnd"/>
</List>
</wt:MessageStrip>
How can I add Button list to a control?
(Button list is in {msgData} object)
MessageStrip.js
sap.ui.define(["sap/m/MessageStrip"],
function (MessageStrip) {
"use strict";
return MessageStrip.extend("com.sap.it.cs.itsdpphome.controller
.fragments.MessageStrip", {
metadata: {
properties: {
},
aggregations: {
},
events: {
}
},
init: function () {
},
renderer:{}
});
});
First of all, you cannot add Button to a List. You have to use sap.m.CustomListItem to put a Button as content.
Let's get to the part about how to meet your current requirement for custom control.
you have define a aggregations for your MessageStrip to put your List
sap.ui.define(["sap/m/MessageStrip"],
function (MessageStrip) {
"use strict";
return MessageStrip.extend("com.sap.it.cs.itsdpphome.controller.fragments.MessageStrip", {
metadata: {
properties: {
},
aggregations: {
list: { type: "sap.m.ListBase", multiple: false }
},
events: {
}
},
init: function () {
MessageStrip.prototype.init.call(this);
},
renderer: {}
});
});
Then you define your own Renderer which extends sap/m/MessageStripRenderer for your MessageStrip. In order to render your list inside a MessageStrip, you have to copy some code from sap/m/MessageStripRenderer.
sap.ui.define(['sap/ui/core/Renderer', 'sap/m/MessageStripRenderer'],
function (Renderer, MessageStripRenderer) {
"use strict";
var MessageStripRenderer = Renderer.extend(MessageStripRenderer);
MessageStripRenderer.render = function (oRm, oControl) {
this.startMessageStrip(oRm, oControl);
this.renderAriaTypeText(oRm, oControl);
if (oControl.getShowIcon()) {
this.renderIcon(oRm, oControl);
}
this.renderTextAndLink(oRm, oControl);
//Render your list aggregation
oRm.renderControl(oControl.getAggregation("list"));
if (oControl.getShowCloseButton()) {
this.renderCloseButton(oRm);
}
this.endMessageStrip(oRm);
}
return MessageStripRenderer;
}, true);
I tried the below view XML and it renders like the following screenshot.
<wt:MessageStrip text="DUMMY">
<wt:list>
<List>
<items>
<CustomListItem><Button text="1" /></CustomListItem>
<CustomListItem><Button text="2" /></CustomListItem>
<CustomListItem><Button text="3" /></CustomListItem>
</items>
</List>
</wt:list>
</wt:MessageStrip>
Hope it helps. Thank you!

sapui5, MessageToast.show is not a function, although sap/m/MessageToast has already been imported

I would like to display a messager in sapui5 Application, when onSave is printed or at the init function ist started. but i have always a error in the console and the messageToast do not work.
Errormessage in console:
Uncaught (in promise) TypeError: MessageToast.show is not a function
this is my controller.js:
sap.ui.define(['sap/ui/core/mvc/Controller',
'timeTrackertimeTracker/controller/BaseController',
'sap/ui/model/json/JSONModel',
"sap/m/MessageToast",
"sap/ui/model/odata/ODataModel",
"sap/ui/core/routing/History"
],
function(Controller, BaseController, MessageToast, JSONModel, ODataModel, History) {
"use strict";
//Global variables
//var _oController, oModel, oView;
var Calendarcontroller = BaseController.extend("timeTrackertimeTracker.controller.Calendarform", {
/* =========================================================== */
/* lifecycle methods */
/* =========================================================== */
onInit: function() {
//Store controller reference to global variable
this.getRouter().getRoute("Calendarform").attachPatternMatched(this._onRouteMatched, this);
MessageToast.show("init");
},
/* =========================================================== */
/* event handlers */
/* =========================================================== */
_onRouteMatched: function() {
// register for metadata loaded events
var oModel = this.getModel("appointments");
oModel.metadataLoaded().then(this._onMetadataLoaded.bind(this));
},
_onMetadataLoaded: function () {
// create default properties
var oProperties = {
Id: "Id" + parseInt(Math.random() * 1000000000)
/* duration: "",
resttime: "",
title: "",
starttime: "",
endtime: "",
Description: ""*/
};
// create new entry in the model
this._oContext = this.getModel("appointments").createEntry("/appointments", {
properties: oProperties
,
success: this._onCreateSuccess.bind(this)
});
// bind the view to the new entry
this.getView().setBindingContext(this._oContext, "appointments");
},
onSave: function(oEvent) {
// bind the view to the new entry
//this.getView().setBindingContext(this._oContext);
this.getModel("appointments").submitChanges();
},
_onCreateSuccess: function (oEvent) {
// navigate to the new product's object view
this.getRouter().navTo("AppointmentsList", true);
// unbind the view to not show this object again
this.getView().unbindObject();
// show success messge
/* var sMessage = this.getResourceBundle().getText("newObjectCreated", [ oEvent.Id ]);
MessageToast.show(sMessage, {
closeOnBrowserNavigation : false
});*/
},
/* _onCreateSuccess: function (oAppointment) {
// show success messge
var sMessage = this.getResourceBundle().getText("newObjectCreated", [ oAppointment.Title ]);
MessageToast.show(sMessage, {
closeOnBrowserNavigation : false
});
},
*/
onCancel: function() {
this.onNavBack();
//this.getView().getModel("appointments").deleteCreatedEntry(this._oContext);
},
/**
* Event handler for navigating back.
* It checks if there is a history entry. If yes, history.go(-1) will happen.
* If not, it will replace the current entry of the browser history with the worklist route.
* #public
*/
onNavBack : function() {
/* var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.navTo("AppointmentsList");*/
var oHistory = History.getInstance(),
sPreviousHash = oHistory.getPreviousHash();
// discard new product from model.
this.getModel("appointments").deleteCreatedEntry(this._oContext);
if (sPreviousHash !== undefined) {
// The history contains a previous entry
history.go(-1);
} else {
// Otherwise we go backwards with a forward history
var bReplace = true;
this.getRouter().navTo("AppointmentsList", {}, bReplace);
}
}
});
return Calendarcontroller;
});
Exchange places of parameters MessageToast and JSONModel in function (line 9): in the dependency list sap/ui/model/json/JSONModel mentioned before sap/m/MessageToast