<commit_before>function Products() {
    this.products = {};
}

Products.prototype = {
    constructor: Products,

    addProduct: function (productToAdd) {
        var productName = productToAdd["product_name"];
        this.products[productName] = productToAdd;
    },

    getProduct: function (productName) {
        return this.products[productName];
    }
};

var products = new Products();


$(function () {
    $.get('http://zbiki.ddns.net/products/_search?size=1000', function onSuccess(data) {
        var hits = data["hits"]["hits"];

        hits.forEach(function (hit) {
            var product = hit["_source"];
            products.addProduct(product);
        });
    });
});

var ProductView = Backbone.View.extend({
    tagName: 'tr',

    events: {
        "change input.product_weight": "inputChanged",
        'click button.delete_product': 'remove'
    },

    initialize: function () {
        _.bindAll(this, 'render', 'productChanged', 'inputChanged', 'effectiveValue', 'unrender', 'remove');
        this.model.bind('change', this.render);
        this.model.bind('remove', this.unrender);
    },

    render: function () {
        $(this.el).html(nunjucks.render('product/ProductView.html', {
            weight: this.model.get('weight'), proteins: this.effectiveValue('proteins'),
            carbohydrates: this.effectiveValue('carbohydrates'),
            fats: this.effectiveValue('fats'),
            nutritive_value: this.effectiveValue('nutritive_value')
        }));


        var title;
        if (this.model.get('product_name') != '') {
            title = this.model.get('product_name');
        } else {
            title = 'Search for product...';
        }

        $('.selectpicker', this.el)
            .selectpicker({
                liveSearch: true
            })
            .ajaxSelectPicker({
                ajax: {
                    url: 'http://zbiki.ddns.net/products/_search',
                    dataType: 'json',
                    data: function () {
                        return {
                            "query": {
                                "match": {
                                    "product_name": "{{{q}}}"
                                }
                            }
                        };
                    }
                },
                locale: {
                    emptyTitle: title
                },
                preprocessData: function (data) {

                    var ctr = 0;
                    var hits = data["hits"]["hits"];

                    var foundProducts = [];
                    hits.forEach(function (hit) {
                        ctr++;
                        var productName = hit["_source"]["product_name"];
                        foundProducts.push(
                            {
                                'value': ctr,
                                'text': productName,
                                'disable': false
                            }
                        );
                    });

                    return foundProducts;
                },
                preserveSelected: false
            });

        $('.selectpicker', this.el).change(this.productChanged);

        return this;<commit_msg>Refactor productView<commit_after>function Products() {
    this.products = {};
}

Products.prototype = {
    constructor: Products,

    addProduct: function (productToAdd) {
        var productName = productToAdd["product_name"];
        this.products[productName] = productToAdd;
    },

    getProduct: function (productName) {
        return this.products[productName];
    }
};

var products = new Products();


$(function () {
    $.get('http://zbiki.ddns.net/products/_search?size=1000', function onSuccess(data) {
        var hits = data["hits"]["hits"];

        hits.forEach(function (hit) {
            var product = hit["_source"];
            products.addProduct(product);
        });
    });
});

var ProductView = Marionette.ItemView.extend({
    template: 'product/ProductView.html',
    tagName: 'tr',

    events: {
        "change input.product_weight": "inputChanged",
        'click button.delete_product': 'remove'
    },

    initialize: function () {
        _.bindAll(this, 'render', 'productChanged', 'inputChanged', 'effectiveValue', 'unrender', 'remove');
        this.model.bind('change', this.render);
        this.model.bind('remove', this.unrender);
    },

    serializeData: function() {
        return {
            weight: this.model.get('weight'), proteins: this.effectiveValue('proteins'),
            carbohydrates: this.effectiveValue('carbohydrates'),
            fats: this.effectiveValue('fats'),
            nutritive_value: this.effectiveValue('nutritive_value')
        }
    },

    onRender: function () {
        var title;
        if (this.model.get('product_name') != '') {
            title = this.model.get('product_name');
        } else {
            title = 'Search for product...';
        }

        $('.selectpicker', this.el)
            .selectpicker({
                liveSearch: true
            })
            .ajaxSelectPicker({
                ajax: {
                    url: 'http://zbiki.ddns.net/products/_search',
                    dataType: 'json',
                    data: function () {
                        return {
                            "query": {
                                "match": {
                                    "product_name": "{{{q}}}"
                                }
                            }
                        };
                    }
                },
                locale: {
                    emptyTitle: title
                },
                preprocessData: function (data) {
                    var hits = data["hits"]["hits"];

                    var foundProducts = [];
                    hits.forEach(function (hit) {
                        var productName = hit["_source"]["product_name"];
                        foundProducts.push(
                            {
                                'value': productName,
                                'text': productName,
                                'disable': false
                            }
                        );
                    });

                    return foundProducts;
                },
                preserveSelected: false
            });

        $('.selectpicker', this.el).change(this.productChanged);

        return this;
{'fraction_non_alphanumeric': 0.1264986166615432, 'fraction_numerical': 0.0013833384568090992, 'mean_word_length': 1.283157894736842, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2189010', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1557, 'n_words': 276}