Portal Client Scrip reading server value as undefined

Joe B2
Giga Guru

I've been playing around with our portal for a bit and have gotten a bit stuck on something that looks like it should be simple but I think there is some kind of security thing blocking this.

Context

I am building a page with multiple tabs and embedded data table widgets (widget-data-table). I intend to put a free text search field above each of these to apply a keywords filter and want to use the in built event call within the widget-data-table:

var eventNames = {
      click: 'data_table.click',
      setFilter: 'data_table.setFilter',		
      setKeywords: 'data_table.setKeywords'
};

$scope.$on(eventNames.setKeywords, function(e, keywords){
		console.log("Keyword search triggered: " + keywords);
		$scope.data.keywords = keywords;
		$scope.setSearch(false);
	});

For this to work in my use case though I need to set a dynamic event name for setKeyWords per embeded widget call otherwise one search box will update everthing.

This should be simple enough.I've created a copy of the OOB widget and am passing the event name along with the parameters when calling the widget and have built this into a proof of concept widget:

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	
		data.incidentTableParams = {
           	table: 'incident',
            window_size: 20,
            keywords: '',
        		filter: '',  							
            view: 'kcc_sn_access',
            o: 'opened_at',
            d: 'desc',
            p: 1,
            id: "form",
            show_breadcrumbs: true,
            enable_filter: true,
						keywordsEventName: 'data_table.setIncidentKeywords'
      };	
	
			data.incidents_list = $sp.getWidget('can-widget-data-table', data.incidentTableParams);

})();

This works nicely and the value gets passed to the widget and I can write it to a server side log and read the correct event call.

For some reason any attempt I make to pass this through to the client side (where the eventNames variable lives) passes the value as undefined

$scope.keywordsEventName = $scope.data.keywordsEventName;
console.log("Keywords Trigger: " + $scope.keywordsEventName);

The above just results in:

find_real_file.png

I've tried passing the value to the html bit and then fetching it from there with an ng-init() call on load but that doesn't work either. Calling the above code using a button on the HTML side does read the value in the console so it is there but it cannot be read.

Does anyone have any ideas why the value cannot be read in the client on widget load and how I may get around this please?

4 REPLIES 4

palanikumar
Mega Sage

Hi,

In server side this value is inside data.incidentTableParams object. So your code at client script should be:

$scope.keywordsEventName = $scope.data.incidentTableParams.keywordsEventName;
console.log("Keywords Trigger: " + $scope.keywordsEventName);
Thank you,
Palani

Thank you for your reply.

Unfortunately that isn't it. I should have mentioned that I have translated the input into the variable data.KeywordsEventName

data.keywordsEventName = data.keywordsEventName || '';
			
gs.info('_Portal Scripting Data Table\nkeywordsEventNames: ' + data.keywordsEventName);

The above correctly prints the expected value from data.KeywordsEventName into the log so it contains data.

Putting {{data.KeywordsEventName}} in the HTML also correctly reads the value from the server side. For some reason it just comes through as undefined in the client script.

Aishwarya Thaku
Tera Expert
Tera Expert

Hi @Joe B ,

 

Try declaring the below in client controller -

var c = this;

Then printing the value as c.data.keywordsEventName.

That can be used to bind client script and server script within a widget.

 

As per the Angular JS controllers, when a function defined on a $scope object is called, this is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on. So, inside the function, this and $scope may not be the same.

 

Please mark my answer as helpful or correct, if applicable.

Thanks,

Aishwarya

Thank you for your reply Aishwarya. Unfortunately that didn't do the trick although I hadn't appreciated the potential differences with this and this and $scope in this context so that is helpful thank you.