Sha256: 7592e9bcb0f242e126ea978ebede6f619436a0897a03027b9adf2293e4b18a87

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

$(function() {

    function getTemplateIdFromModelType(modeltype) {
        return "#" + modeltype + "_new_context_form_template"
    }

    function applyContextTemplate(el) {
        // build the template id
        var templateId = getTemplateIdFromModelType($(el).attr("data-model-type"))

        // get the context from the element
        var context = $(el).attr("data-context")

        // get the current value from the element
        var values = $(el).attr("data-value")

        // do it
        createNewContextFormInListItem(el, templateId, context, values)
    }

    function createNewContextFormInListItem(el, fromTemplate, forContext, withValues) {
        // load the template of the element
        var source   = $(fromTemplate).html();

        // compile the template via handlebars
        var template = Handlebars.compile(source);

        // generate the data field
        var data = { context: forContext, context_attribute: forContext.singularize(), values: withValues};

        // attach the formular to the element
        $(el).append(template(data))
    }


    $('li[data-context]').each(function(index, value) {
        applyContextTemplate(value)
    })

    $('div#act-as-taggable-dynamic-new_context_form a').click(function() {
        // get the value
        var value = $('div#act-as-taggable-dynamic-new_context_form a').parent().find('input').val()

        // get the model type
        var modeltype = $('div#act-as-taggable-dynamic-new_context_form a').parent().find('input').attr('data-model-type')

        // read out the model type
        var templateId = getTemplateIdFromModelType(modeltype)

        // find the list for this model type
        var list = $("ul#" + modeltype + "_tag_context_form")

        // create the list item we need
        var listItem = $('<li></li>').appendTo(list)

        // generate a new context
        createNewContextFormInListItem(listItem , templateId, value, "")
    })
})

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts-as-taggable-on-dynamic-0.0.3 lib/assets/javascripts/taggable.dynamic.js
acts-as-taggable-on-dynamic-0.0.2 lib/assets/javascripts/taggable.dynamic.js
acts-as-taggable-on-dynamic-0.0.1 lib/assets/javascripts/taggable.dynamic.js