Skip to Content
0
Former Member
May 16, 2016 at 12:52 PM

How to access component properties in createContent

242 Views

Hi All,

I have a reusable component which can be instantiated in other applications using

sap.ui.getCore().createComponent({
  name: "sample.service",
  id : "a12345",
  settings:{
   key: "A9320"
  }

In my component.js, I want to be able to access the value of the property key inside createContent to change the view being loaded according to the value of the key.

var Component = UIComponent.extend("sample.service.Component", {
  metadata : {
  "manifest" : "json",
  "library" : "sample.service",
  properties : {
  key : {type: 'string', group:  'Misc', defaultValue:null},


  }



  },
  onBeforeRendering:function()
  {
     console.log(this.getKey());
//    this.page = new sap.ui.view({ viewName:"sample.service.view.View2", type:sap.ui.core.mvc.ViewType.XML});
     
  },
  init : function()
  {
        UIComponent.prototype.init.apply(this, arguments);
        var a;


  },
  createContent: function(){
            console.log(this.getKey());
            
     if(this.getKey() === "a123")
     {
          
  this.page = new sap.ui.view({ viewName:"sample.service.view.View1", type:sap.ui.core.mvc.ViewType.XML});
     }
     else
     {
       this.page = new sap.ui.view({ viewName:"sample.service.view.View2", type:sap.ui.core.mvc.ViewType.XML});
     }
  return this.page;  
  },
});

Although the getters and setters are defined automatically for the property, if I call this.getKey() inside createContent, it returns an empty string.

I can access the key value inside the controller of the view after the view is instantiated but not in init() or createContent().

this.getKey() returns the value in onBeforeRendering() also. Is it possible to change the view being loaded at this point?

The development toolkit mentions that a JSON object componentData can be accessed inside createContent.How do I access this object? Where are the parameters defined?

SAPUI5 SDK - Demo Kit

Thanks and Regards,

VIjay