Skip to content

Commit

Permalink
select2ajax: use requirejs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bertrand Mathieu committed Jun 16, 2015
1 parent ff9d0d4 commit b3dd2f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions abilian/web/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,9 @@ def process_formdata(self, valuelist):
return valuelist

def __call__(self, field, **kwargs):
"""
Render widget
"""
if self.multiple:
kwargs['multiple'] = True

Expand Down
14 changes: 10 additions & 4 deletions abilian/web/templates/widgets/select2ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{%- deferJS %}
<script type="text/javascript">
(function() {
function initWidget($) {
var init_params = {
minimumInputLength: 2,
containerCssClass: 'form-control',
Expand All @@ -14,8 +14,8 @@
var data = [{% for oid, label in values %}
{id: {{ oid|tojson }}, text: {{ label|tojson }}}{% if not loop.last %},{%- endif %}{% endfor %}
];
if (data.length > 1) debugger;
if (data.length > 0) {
if (data.length > 1) { /* debugger; */ }
else if (data.length > 0) {
callback(data{%- if not s2_params.multiple %}[0]{%- endif %});
}
},
Expand All @@ -37,6 +37,12 @@
$('input[name="{{ id }}"]').data({'init-with': 'select2',
'init-params': init_params}
);
})();
}

require(['Abilian', 'jquery'],
function(Abilian, $) {
console.log('register onAppInit');
Abilian.fn.onAppInit(function() { initWidget($); });
});
</script>
{%- enddeferJS %}

0 comments on commit b3dd2f5

Please sign in to comment.