我对ajax小部件有一个真正的noob问题。这实际上是我刚才问的一个问题的延伸:ajax widgets in pyramid and chameleon模板语言和ajax小部件
如果你看这个问题和文件account_login_widget.pt的内容,你会看到'widget'使用类似$ {username}的语法。这再次适用于一个页面上只有一个登录窗口小部件,但所有地狱崩溃尝试使用这个“窗口小部件模式”的小部件,可以在页面上多次存在。
现在我想创建一个ajax切换按钮。该按钮应该是可配置的:按钮的文本可以根据其使用的页面而改变,并且回调代码也应该能够被改变......再次取决于页面。另外,多个切换按钮应该能够存在于同一页面上。
2个问题:
比方说,我有我的网页上的餐馆和“喜欢”按钮旁边的每个列表。将餐厅ID传递给'post'服务器调用的正确方法是什么(即:如何将变量传递给javascript部件?)。
在同一页上,假设我想使用相同的ajax切换按钮小部件,但在不同的上下文中。比方说,在同一页上,我也有一个人名单,我想'追随'他们。将按钮文本变量传递到窗口小部件并更改post服务器调用行为的正确方法是什么? (即:在问题1也许我想打电话给restaurant.like(),但问题2我想打电话给person.follow()
下面是该控件的一些示例代码...
<script type="text/javascript">
// plugin implementation
(function($) {
$.fn.create_ajax_toggle_button = function(csrf, name, toggle_on, url, on_text, off_text) {
return this.each(function() {
var anchor = $('<a></a>');
anchor.appendTo(this);
$(anchor).button();
$(anchor).toggle_ajax_button_text(toggle_on, on_text, off_text);
$(anchor).click(function(e) {
var form_data = '_csrf=' + csrf + '&name=' + name + '&toggle_on=' + toggle_on;
$.post(url, form_data, function(response) {
$.fn.toggle_ajax_button_text();
});
});
});
};
$.fn.toggle_ajax_button_text = function(toggle_on, on_text, off_text) {
if (toggle_on == 'True') {
$(this).toggleClass('ui-state-active');
$(this).text(on_text);
} else {
$(this).text(off_text);
}
};
})(jQuery);
// Define the entry point
$(document).ready(function() {
// YUCK!!! I really shouldn't be/can't using ${name} templating
// syntax like this. Doing this means the JS code needs to be
// loaded over and over again for each item in the list. This
// just doesn't work. :-(
$('.ajax_toggle_button_div_${name}').create_ajax_toggle_button('${csrf}', '${name}', '${toggle_on}', '${url}', '${on_text}', '${off_text}');
});
</script>
<div class="ajax_toggle_button_div_${name}"></div>
谢谢!
感谢您的详细答复。这比我想要达到的要少一些,但我最终可能只是走这条路。 – lostdorje 2012-01-02 02:13:01