Problem with Onclick Javascript in Template field

  • 1
  • Question
  • Updated 2 years ago
I have created one Template field in table. Below is the code. But the code is not working properly. i have set the value of allow HTML to true.

<html>
<head>

<script>
    function test(){
        alert('Hiiii0');
        var $ = skuid.$, args = arguments[0],item = args.item,list = args.list,model = args.model;
         alert('Hiiii1');
        var currentUserModel = skuid.model.getModel( 'UnReadModelPA' );
        alert('Hiiii2');
        var currentUserReadModel1 = skuid.model.getModel( 'ReadModelDesktop' );
        alert('Hiiii3');
        var currentUserReadModel2 = skuid.model.getModel( 'ReadModelOnline' );
        alert('Hiiii4');
        var alertId = currentUserModel.getFieldValue( item.row, 'Id' ) ;
        alert('Hiiii5');
        MyApexController.getUnReadAlerts(alertId, function(result, event) {
            if (event.type == 'exception') {
                skuid.jQuery.unblockUI();
                alert(event.message);
            }else{
         skuid.model.updateData([currentUserModel],function(){
                    skuid.jQuery.unblockUI();
                }); 
              skuid.model.updateData([currentUserReadModel1],function(){
                    skuid.jQuery.unblockUI();
                });
              skuid.model.updateData([currentUserReadModel2],function(){
                    skuid.jQuery.unblockUI();
                });
            }
        });
    }
</script>
</head>
<body>
<p onClick="test()">{{Alert_Message__c}}</p>
</body>
</html>


I am getting only first alert(alert('Hiiii0');) , but then it just dont work. 

Can someone help me to debug this?
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb

Posted 2 years ago

  • 1
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb
After investigation i found that Skuid does not recognize $ sign for jQuery. Hence i have used jQuery now .

var $ = skuid.jQuery, args = arguments[0],item = args.item,list = args.list,model = args.model;

but now i am not getting any value in arguments[0].

Can somebody help to resolve this out?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Sumeet,

What is the goal here? Why write your own scripts inside a template instead of just defining a javascript snippet in the pagebuilder?
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb
Hi Matt,

I even tried that like calling JavaScript snippet in my html template using skuid.snippet.getSnippet('snippetName')();

But still I'm not getting anything in arguments[0].
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
What's the use case? Why do you need to call a snippet from a template component?

Does your template component have a model assigned? It may just be that you have no context for skuid to send to the arguments.
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb
I want to perform some operation on click of field in my model. So I put that field value in template and called on click function in HTML template. The template is inside table only and table is assigned by model of custom object. Do I need to assign model to my template if yes , how?

Thanks for quick response!!
Photo of Sumeet Sangle

Sumeet Sangle

  • 452 Points 250 badge 2x thumb
you can see the main question body to know what I am actually doing and how my code look like.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Instead of putting your field in a template, use a custom field renderer. There you can add a onclick function to field.element.