0

Is there a way to configure a Javascript(which accepts few parameters) and initialize a function with user specified parameters in an unobtrusive way with data-attributes?

Any reference would be great help

 InitializeLogging({
  Url: "/xxx/yyyy",
  x: 10,
  y: true,
  somevalue: false,
  unHandledErrorCallback: function (message) {
   console.log("Somerror message " + message);
  }
  });
SCP
  • 63
  • 1
  • 7

1 Answers1

0

We can achieve that in the following way,

HTML

<a href="javascript:void(0);" data-attributes="a">Call a</a> <br />
<a href="javascript:void(0);" data-attributes="b">Call b</a>

Script

var app = {
    init: function(){
        this.addEventListener();
    },

    a: function(){ alert("You have called 'a'"); },

    b: function(){ alert("You have called 'b'"); },

    callMethod: function(event){
        var func = $(this).attr('data-attributes');
        app[func].call();
    },

    addEventListener: function(){
        $('a').on('click', this.callMethod);
    }
};

$(document).ready(function(){
    app.init();
});

Demo JS http://jsfiddle.net/DfHXs/2/

Hope this will help you.

moustacheman
  • 1,424
  • 4
  • 21
  • 47
  • Thanks..I have updated my question.Exactly I am looking for html helper to initilize the above JS function in View via html extension – SCP May 05 '13 at 15:19