Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

javascript - Passing parameters on JQuery .trigger

I am using JQuery trigger but am not sure what the correct syntax is to pass parameters in my situation. Here is where I am making the call :

$('#'+controlName).trigger(event);

Here is where I am doing the event binding :

$(window).on('onPartialRendered', onPartialRendered);

And here is my event handler :

var onPartialRendered = function () {

    .....
};

Everything works fine until I try to pass parameters. What would be the correct way to do it as per my example?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First parameter is always string with event name and next parameters are additional data:

.trigger('foo', [1, 2]);

.on('foo', function(event, one, two) { ... });

Special thanks for Rocket Hazmat

Example:

var controller = {
  listen: function (event, json, string) {}
};

$('body').trigger('this_works', [{data: [1, 2, 3]}, 'something']);

$('body').on('this_works', function (event, json, string) {
  controller.listen(event, json, string);
});

Remote partial:

Please do not use this way. There is many article about this problem in network. This take much time and generate unnecessary traffic in network. Please use this way:

var template = $('#templates #example_template').detach();
var clone = template.clone();
clone.find('.some_field').val('new_data');
clone.attr('id', null);
$('table tbody').append(clone);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...