Skip to content

org.got5.tapestry5.jquery.utils.JQueryUtils#merge should respect recursive json objects #325

@donarus

Description

@donarus

Hi, if someone needs to set second level jQuery property (for example properties oLanguage.oPaginate.sPrevious and oLanguage.oPaginate.sPrevious which can be used in DataTable) and uses method JQueryUtils#merge, he will ends up with
with partially rewritten options instead of merged options. JAVADoc and method name is little bit misleading

We can change method behaviour or (from backward compatibility reasons) add recursive (stack based) method with no such strict merge restrictions.

Example of ugly merging (see comments in code)

package cz.donarus.tagr.web.components.bootstrap;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.got5.tapestry5.jquery.utils.JQueryUtils;

@Import(library = "DataTable.js")
public class DataTable extends org.got5.tapestry5.jquery.components.DataTable {

    @Inject
    private Messages messages;

    @Override
    public JSONObject getOptions() {
        JSONObject originalPaginate = originalPaginate = new JSONObject();
        originalPaginate.put("sPrevious", messages.get("datatable.oPaginate.sPrevious"));
        originalPaginate.put("sNext", messages.get("datatable.oPaginate.sNext"));

        JSONObject originalLanguage = new JSONObject();
        originalLanguage.put("oPaginate", originalPaginate);

        /*
        FIXME: UGLY COPY PASTE FROM org.got5.tapestry5.jquery.components.DataTable, because without these lines
        call of JQueryUtils#merge in parent#setJS method will overwrite all oLanguage values by
         our returned properties (so without these lines only with pagination titles).
         */
        originalLanguage.put("sProcessing", messages.get("datatable.sProcessing"));
        originalLanguage.put("sLengthMenu", messages.get("datatable.sLengthMenu"));
        originalLanguage.put("sZeroRecords", messages.get("datatable.sZeroRecords"));
        originalLanguage.put("sEmptyTable", messages.get("datatable.sEmptyTable"));
        originalLanguage.put("sLoadingRecords", messages.get("datatable.sLoadingRecords"));
        originalLanguage.put("sInfo", messages.get("datatable.sInfo"));
        originalLanguage.put("sInfoEmpty", messages.get("datatable.sInfoEmpty"));
        originalLanguage.put("sInfoFiltered", messages.get("datatable.sInfoFiltered"));
        originalLanguage.put("sInfoPostFix", messages.get("datatable.sInfoPostFix"));
        originalLanguage.put("sSearch", messages.get("datatable.sSearch"));
        originalLanguage.put("sUrl", messages.get("datatable.sUrl"));
        /*
        FIXME: END OF UGLY COPY PASTE
         */

        JSONObject options = new JSONObject();
        options.put("oLanguage", originalLanguage);
        JQueryUtils.merge(options, super.getOptions());
        return options;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions