如果在对象中定义,则覆盖函数其他默认功能

表达x

calendar有用TypeScript. 我可以将侦听器绑定到单独的函数,但我希望这个单独的函数具有默认功能,直到有人覆盖config传递给构造函数的对象中的函数。在构造函数中,我传递了类似的东西

{
  container: 'xxx',
  cellClick: function(sender, event, data) {
    // custom functionality
  }
}

我如何cellClick在我的基类中定义一个函数,当我呈现我的td单元格以将事件侦听器绑定到此函数并为单元格传递一些数据时,这些数据将为单元格提供一些默认功能,但如果有人在对象中定义了该函数用单击的单元格的数据覆盖它。

我的做法与此类似,但这会将侦听器直接绑定到config函数。我敢打赌,在我的setter身上做到这一点真的很容易,但我不知道如何做到。

td.addEventListener('click', function () {
  this.config.cellClick(td, event, data);
})

编辑:这是我的abstract类,它扩展了Table类,它只是为thead, tbody and tfoot

defaultConfig = {
                container: '',
                dataSource: [],
                currentDate: new Date(),
                currentView: 'month',
                views: [
                    { type: 'Day' },
                    { type: 'Week' },
                    { type: 'Month' }
                ],
                startDayHour: 8,
                endDayHour: 20,
                cellDuration: 60,
                todayHighlight: true,
                cellClick: this.cellClick,
                eventClick: this.eventClick,
                showAllDayPanel: false,
                disabledDays: [],
                resources: {
                    dataSource: [],
                    field: null
                },
                groupBy: null,
            }

            set config(cfg: my.core.calendar.iCalendarConfiguration) {
                if (cfg) {

                    for (var key in cfg) {
                        if (cfg[key] == null) {
                            cfg[key] = this.defaultConfig[key];
                        }
                    }

                    if (cfg.cellClick != null) {
                        cfg.cellClick.bind(this.cellClick);
                    }

                    this.configuration = cfg;
                    this.MonthLength = new Date(cfg.currentDate).getMonth();
                    this.weekDayStart = cfg.currentDate;
                    this.weekDayEnd = cfg.currentDate;
                }
            }

            get config(): iCalendarConfiguration {
                return this.configuration;
            }

            .............

            constructor(config: iCalendarConfiguration) {
                super();

                this.element.className = "table table-bordered table-responsive col-sm-12"; // could be readed from config but tbd
                this.config = config;
                this.appointmentsArray = config.dataSource;
                this.tools = new my.calendar.CalendarTools();

                /**
                 *  Append the element to the container
                 *  which is defined in the config.
                 */
                document.getElementById(config.container).appendChild(this.tools.createDiv('', 'row', this.parentID)).appendChild(this.element);
            }

            abstract createCalendar();

            abstract bindAppointments(view: string);

            abstract Next(sender, e, data)

            abstract Previous(sender, e, data)

            abstract TabClick(sender: any, event: any, data: any)

            abstract initialize()

            abstract onResize()

            abstract cellClick(sender: any, event: any, data: any)

            abstract eventClick(sender: any, event: any, data: any)

并且有一个类calendar扩展了抽象类及其使用配置对象实例化的类。

export class Calendar extends my.core.calendar.CalendarTable {
        weeklyView: my.calendar.WeeklyView;
        monthlyView: my.calendar.MontlyView;
        dayView: my.calendar.DayView;

        constructor(cfg: my.core.calendar.iCalendarConfiguration) {
            super(cfg);

            this.weeklyView = new my.calendar.WeeklyView(this);
            this.monthlyView = new my.calendar.MontlyView(this);
            this.dayView = new my.calendar.DayView(this);
        }


        onResize() {
            /**
             *  Repaint all appointments on window resize
             *  For many reasons
             */
            this.bindAppointments();
        }

        createCalendar() {

            /** Clear everything on change */
            if (this.tBody.rows.length > 0) {
                this.tBody.clear();
                this.tHead.clear();
            }

            switch (this.config.currentView) {
                case "month":
                    this.monthlyView.createMontlyView();
                    break;
                case "day":
                    this.dayView.createDayView();
                    break;
                case "week":
                    this.weeklyView.createWeeklyView();
                    break;
            }

            this.bindAppointments();
        }

        bindAppointments() {
            /** Remove the events div for week/day view. Its here because reasons. */
            if (this.element.parentElement.querySelector("#events") !== null) {
                let child = document.getElementById('events');

                this.element.parentElement.removeChild(child);
            }

            switch (this.config.currentView) {
                case "month":
                    this.monthlyView.bindMonthAppointments();
                    break;
                case "day":
                    this.dayView.bindDayAppointments();
                    break;
                case "week":
                    this.weeklyView.bindWeekAppointments();
                    break;
            }
        }

        Next(sender, e, data) {
            switch (this.config.currentView) {
                case "month":
                    this.monthlyView.monthNavigationChange(true);
                    break;
                case "day":
                    this.dayView.dayNavigationChange(true);
                    break;
                case "week":
                    this.weeklyView.weekNavigationChange(true);
                    break;
            }
            this.createCalendar();
            this.updateLabels();
        }

        Previous(sender, e, data) {
            switch (this.config.currentView) {
                case "month":
                    this.monthlyView.monthNavigationChange(false);
                    break;
                case "day":
                    this.dayView.dayNavigationChange(false);
                    break;
                case "week":
                    this.weeklyView.weekNavigationChange(false);
                    break;
            }
            this.createCalendar();
            this.updateLabels();
        }

        TabClick(sender: any, event: any, data: any) {

            switch (sender.id.toLowerCase()) {
                case "day":
                    this.tools.setActiveTab(this, 'day', event);
                    // update currentdate
                    break;
                case "month":
                    this.tools.setActiveTab(this, 'month', event);
                    // update currentdate
                    break;
                case "week":
                    this.tools.setActiveTab(this, 'week', event);
                    // update currentdate
                    break;
            }
            this.createCalendar();
            this.updateLabels();
        }


        updateLabels() {
            let date = new Date(this.config.currentDate);
            switch (this.config.currentView) {
                case "month":
                    this.currentDateMonth.value = String(this.calendar_months_label[date.getMonth()]) + ' ' + String(date.getFullYear());
                    break;
                case "day":
                    this.currentDateMonth.value = String(this.config.currentDate.getDate()) + ' ' + String(this.calendar_months_label[this.config.currentDate.getMonth()]) + ' ' + String(this.config.currentDate.getFullYear());
                    break;
                case "week":
                    this.currentDateMonth.value = String(this.tools.getPreviousWeekStr(this.weekStart, this.weekEnd, this.calendar_months_label[this.config.currentDate.getMonth()], this.config.currentDate.getFullYear()));
                    break;
            }
        }

        initialize() {
            this.tools.createNavigation(this, this.config);
            this.createCalendar();
            this.bindAppointments();
        }

        cellClick(sender, event, data) {
            console.log('fired up from mycalendar');
        }

        eventClick(sender, event, data) {

        }

    }; // end class basic

还有 3 个用于view渲染、逻辑等的,它们获取表对象并执行一些操作。

这是我初始化它的方式。

var calendar = new my.calendar.Calendar({
        container: 'calendarTestContainer',
        dataSource: data,
        views: [
            { type: 'Week' },
            { type: 'Day' },
            { type: 'Month' }
        ],
        currentDate: new Date('2017-03-15'),
        currentView: 'week',
        startDayHour: 8,
        disabledDays: [6],
        cellDuration: 30,
        resources: {
            dataSource: staffs,
            field: 'UID'
        },
        endDayHour: 24,
        cellClick: function (sender, e, data) {
            // this has to be overrided
        },
        showAllDayPanel: true
    }).initialize();

由于我并不是真正在 OOP 中,因此我愿意接受重构建议。

D B

您可以为默认对象提供默认功能:

defaults = {
    option1: 'value1',
    cellClick: function() {/*do your default stuff here*/}
}

当您拥有配置对象时,您可以使用Object.assign以下命令合并它们

Object.assign(defaults, config || {});

您的defaults对象现在包含所有默认值,除非您的config对象中有值现在你可以使用

td.addEventListener('click', defaults.cellClick)

这会将您的默认函数或覆盖函数添加为处理程序。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果在反应中通过对象json映射时出现其他条件

如果在Infopath(Xpath)中则其他:如何?

如果在其他印刷声明中

如果在python中需要减少其他

如果在对象数组中将对象设置为未定义,则返回 true

图像中存储的像素值会自动更改。如果在其他功能中再次打开图像

从对象中提取值(如果在对象中)-参考错误

如果在对象数组中不为真,javascript更新属性值

如果在对象内找到值,则返回键

如果在其他两个类中不存在新对象,如何在Python Django中创建一个新对象?

PySpark:如果在数据帧中的链中,如何处理“其他”?

如果在Python列表中的map()中其他地方如何使用lambda?

删除对对象的引用,但如果在其他位置引用,则将对象保留在内存中

如果在HTML5中按下了“其他”按钮?

如果在render return中其他情况下如何写反应本机

如果在ng级中还有其他条件?

PHP,如果在WordPress模板中还有其他-这是php吗?

如果在iOS中的其他线程上设置了视图的属性,则如何刷新视图

如果在Java中,如何继续进行其他操作?

如果在ggplot中以其他条件添加一个额外的图层

SQL如果在SQL中存在其他条件,如何添加

如果在一个表中为null,则从“其他”中选择

如果在Python中对多个变量进行其他操作则更干净

如果在Visual Force页面中阻止其他内容

如果在java中我应该如何避免其他

如果在超类中定义了方法,则如何根据调用该对象的对象更改结果

如果在AND中用AND或OR进行其他处理

如果在其他方面?

SQL聚合方法返回字符串(如果在集合中),或任何其他字符串(如果在集合中)