Skip to Content
0

Use model defined in component.js outside controller

Nov 01, 2016 at 03:32 PM

137

avatar image

Hi,

I defined a model inside component and can access it successfully inside my view controller.

In Init of my controller I am calling another abc.js file and want to use the same model defined in component inside it. Tried using this.getOwnerComponent().getModel("dbmodel"); but it gives error saying TypeError: this.getOwnerComponent is not a function.

I think the error is because my abc.js and component are not connected. I also tried adding jQuery.sap.require("testmodel.Component"); inside abc.js but of no use.

Please let me know where am I doing wrong?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
avatar image
Former Member Nov 02, 2016 at 09:28 PM
0

ok....then you need to call the function which is defined in abc with the Controller as parameter and then call in the function.

As mention in this you do not have the Controller is seams. :)

Controllerparameter.getOwnerComponent().getModel(modelname)

please let me know if this works out for you.

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Nov 01, 2016 at 09:14 PM
0

Hi,

please define you model in the manifest.json in the model section ... see example for json model and datasource model(odata)

and then you can call it with

this.getOwnerComponent().getModel("nameOfModel");

please also check if you app runs with sap version 1.3x version as getModel is only support starting with this version with getOwnerComponent.

What you have in this? Please ensure it is the controller.

json models

"models": {

"i18n": {

"type": "sap.ui.model.resource.ResourceModel",

"settings": { "bundleName": "sap.app.i18n.i18n" }

},

"dbModel": {

"type": "sap.ui.model.json.JSONModel",

"settings": {},

"uri": "model/modelname.json", "preload": true }

},

odata model .. in section sap.app in manifest.json

"dataSources":

{ "dbModel":{

"uri": "/odata/v2/odata/v2/",

"type": "OData",

"settings": { "odataVersion": "2.0", "localUri": "model/metadata.xml" } } }

Share
10 |10000 characters needed characters left characters exceeded
Justin Kemp Nov 02, 2016 at 03:38 PM
0

I have already done this and am able to access model in view controller(version 1.36). Want to access model inside abc.js called in init method of view controller.

,

this is already done, and am able to get the model in view controller init method(the version is above 1.3x) I want to use it inside another file abc.js which is called in view controller.

Share
10 |10000 characters needed characters left characters exceeded