Skip to Content

Use model defined in component.js outside controller

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?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

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

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Nov 01, 2016 at 09:14 PM

    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" } } }

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 02, 2016 at 03:38 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded