Test Setup Failed
Push — master ( cd1dab...2a689d )
by
unknown
03:51
created

ShoppingListCollectionComponent   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
define(function(require) {
2
    'use strict';
3
4
    var ShoppingListCollectionComponent;
5
    var BaseComponent = require('oroui/js/app/components/base/component');
6
    var BaseCollection = require('oroui/js/app/models/base/collection');
7
    var ShoppingListCollectionService = require('oroshoppinglist/js/shoppinglist-collection-service');
8
    var mediator = require('oroui/js/mediator');
9
    var _ = require('underscore');
10
11
    ShoppingListCollectionComponent = BaseComponent.extend({
12
        /**
13
         * @inheritDoc
14
         */
15
        listen: {
16
            'shopping-list:line-items:update-response mediator': '_onLineItemsUpdate'
17
        },
18
19
        /**
20
         * @inheritDoc
21
         */
22
        constructor: function ShoppingListCollectionComponent() {
23
            ShoppingListCollectionComponent.__super__.constructor.apply(this, arguments);
24
        },
25
26
        /**
27
         * @inheritDoc
28
         */
29
        initialize: function(options) {
30
            this.collection = new BaseCollection(options.shoppingLists);
31
            this.collection.comparator = this.comparator;
32
33
            this.collection.on('update', function(collection, options) {
34
                if (options.add) {
35
                    mediator.trigger('shopping-list:refresh');
36
                }
37
            });
38
            this.collection.on('change', function(options) {
39
                if (options && options.refresh) {
40
                    mediator.trigger('shopping-list:refresh');
41
                }
42
            });
43
            ShoppingListCollectionService.shoppingListCollection.resolve(this.collection);
44
        },
45
46
        comparator: function(model) {
47
            // 0 for current SL - should be first
48
            return model.get('is_current') ? 0 : model.get('id');
49
        },
50
51
        _onLineItemsUpdate: function(model, response) {
52
            if (!model || !response) {
53
                return;
54
            }
55
56
            if (response.message) {
57
                mediator.execute(
58
                    'showFlashMessage', (response.hasOwnProperty('successful') ? 'success' : 'error'),
59
                    response.message
60
                );
61
            }
62
63
            var updateModel = function(model, product) {
64
                model.set('shopping_lists', product.shopping_lists, {silent: true});
65
                model.trigger('change:shopping_lists');
66
            };
67
            if (response.product && !_.isArray(model)) {
68
                updateModel(model, response.product);
69
            } else if (response.products && _.isArray(model)) {
70
                model = _.indexBy(model, 'id');
71
                _.each(response.products, function(product) {
72
                    updateModel(model[product.id], product);
73
                });
74
            }
75
76
            if (response.shoppingList && !this.collection.find({id: response.shoppingList.id})) {
77
                this.collection.add(_.defaults(response.shoppingList, {is_current: true}), {
78
                    silent: true
79
                });
80
            }
81
82
            this.collection.trigger('change', {
83
                refresh: true
84
            });
85
        }
86
    });
87
88
    return ShoppingListCollectionComponent;
89
});
90