Source: models/uibuilder/watchall.js

define('application/models/uibuilder/watchall', [
    'rofl/models/api/abstract',
    'application/managers/api',
    'application/models/broadcast',
    'rofl/lib/utils'
], function (
    Class,
    ApiManager,
    Broadcast,
    Utils
) {
    'use strict';

    var api = ApiManager.getKPNAPI();

    return Class.extend({

        /**
         * Initialises the model.
         *
         * @param {Object} data - The API data.
         */
        init: function (data) {
            this._title = data.title;
            this._apiEndpoint = '101/1.2.0/A/nld/smarttv/kpn' + data.url;
            this._layout = data.layout;
        },

        /**
         * Returns the type of widget this model to be used with.
         *
         * @returns {string} - The type of widget.
         */
        getWidgetType: function () {
            return 'watchall';
        },

        /**
         * Returns the title.
         *
         * @returns {string} - The title.
         */
        getTitle: function () {
            return this._title;
        },

        /**
         * Returns the api endpoint.
         *
         * @returns {string} - The api endpoint.
         */
        resolveEndpoint: function () {
            return this._apiEndpoint;
        },

        /**
         * Prepares the widget and loads all required data.
         *
         * @returns {Promise} - Promise resolving with the carousel model.
         */
        prepare: function () {
            return api.read(this, {
                withCredentials: true
            })
                .then(Utils.bind(function (response) {
                    var bookmarks = response.resultObj.bookmarks || [],
                        contentType;

                    if (this._layout === 'BOOKMARK_LARGE_HORIZONTAL' && bookmarks.length) {
                        contentType = 'vod';

                        return api.read('searchall', {
                            params: {
                                query: '',
                                filter_contentIds: this._getContentIdsFromBookmarks(bookmarks),
                                _searchContentType: contentType
                            }
                        }).then(Utils.bind(function (searchResponse) {
                            this._setDataItems(searchResponse, true);

                            return this;
                        }, this));
                    }

                    this._setDataItems(response);

                    return this;
                }, this));
        },

        /**
         * Returns the items.
         *
         * @returns {Array} - The items to be appended.
         */
        getItems: function () {
            return this._items;
        },

        /**
         * Retrieve and/or set the received data items.
         *
         * @param {Object} response - Response from the requested endpoint.
         * @param {Object} transformedData - True if data is already transformed, no need to create Broadcast.
         * @private
         */
        _setDataItems: function (response, transformedData) {
            var items = [],
                containers;

            if (transformedData) {
                items = response;
            } else {
                containers = response.resultObj.containers;

                Utils.each(containers, function (container) {
                    items.push(new Broadcast(container));
                });
            }

            this._items = items;
        },

        /**
         * Retrieves content ids from the bookmarks response.
         *
         * @param {Object} bookmarks - The bookmarks response.
         * @returns {Array} - Bookmarks' content ids list.
         * @private
         */
        _getContentIdsFromBookmarks: function (bookmarks) {
            var contentIds = [],
                bound = Utils.bind(function (bookmark) {
                    contentIds.push(bookmark.bookmarkSet.contentId);
                }, this);

            Utils.each(bookmarks, bound);

            return contentIds;
        }
    });
});