Util

Class Diagram

Inheritance diagram of django_select2.util

Reference

class django_select2.util.JSVar[source]

Bases: unicode

A JS variable.

This is a simple unicode string. This class type acts as a marker that this string is a JS variable name, so it must not be quoted by convert_py_to_js_data() while rendering the JS code.

class django_select2.util.JSFunction[source]

Bases: django_select2.util.JSVar

A JS function name.

From rendering point of view, rendering this is no different from JSVar. After all, a JS varible can refer a function instance, primitive constant or any other object. They are still all varibles.

Tip

Do use this marker for JS functions. This will make the code clearer, and the purpose more easier to understand.

class django_select2.util.JSFunctionInContext[source]

Bases: django_select2.util.JSVar

A JS function name to run in context of some other Html DOM element.

Like :py:class:JSFunction`, this too flags the string as JS function, but with a special requirement. The JS function needs to be invoked in the context of a Html DOM, such that, this inside the function refers to that DOM instead of window.

Tip

JS functions of this type are warapped inside special another JS function – django_select2.runInContextHelper.

django_select2.util.render_js_script(inner_code)[source]

This wraps inner_code string inside the following code block:

<script>
    $(function () {
        // inner_code here
    });
</script>
Return type:unicode
django_select2.util.extract_some_key_val(dct, keys)[source]

Gets a sub-set of a dict.

Parameters:
  • dct (dict) – Source dictionary.
  • keys (list or any iterable.) – List of subset keys, which to extract from dct.
Return type:

dict

django_select2.util.convert_py_to_js_data(val, id_)[source]

Converts Python data type to JS data type.

Practically what this means is, convert False to false, True to true and so on. It also takes care of the conversion of JSVar, JSFunction and JSFunctionInContext. It takes care of recursively converting lists and dictionaries too.

Parameters:
Return type:

unicode

django_select2.util.convert_dict_to_js_map(dct, id_)[source]

Converts a Python dictionary to JS map.

Parameters:
Return type:

unicode

django_select2.util.convert_to_js_arr(lst, id_)[source]

Converts a Python list (or any iterable) to JS array.

Parameters:
Return type:

unicode

django_select2.util.convert_to_js_string_arr(lst)[source]

Converts a Python list (or any iterable) of strings to JS array.

convert_to_js_arr() can always be used instead of this. However, since it knows that it only contains strings, it cuts down on unnecessary computations.

Return type:unicode
django_select2.util.synchronized(f)[source]

Decorator to synchronize multiple calls to a functions.

django_select2.util.is_valid_id(val)[source]

Checks if val is a valid generated Id.

Parameters:val (str) – The value to check.
Return type:bool
django_select2.util.register_field(*args, **kwargs)[source]

Registers an Auto field for use with views.AutoResponseView.

Parameters:
  • key (unicode) – The key to use while registering this field.
  • field (AutoViewFieldMixin) – The field to register.
Returns:

The generated Id for this field. If given key was already registered then the Id generated that time, would be returned.

Return type:

unicode

django_select2.util.get_field(id_)[source]

Returns an Auto field instance registered with the given Id.

Parameters:id (unicode) – The generated Id the field is registered with.
Return type:AutoViewFieldMixin or None

Table Of Contents

Previous topic

Views

This Page