简体   繁体   English

AlloyUI Scheduler - 从数据库加载后添加自定义属性

[英]AlloyUI Scheduler - add custom attribute after loading from database

I've been struggling with this issue for a couple of days now, basically what I'm trying to accomplish is to add a custom attribute to each event that I load from the database.我已经为这个问题苦苦挣扎了几天,基本上我想要完成的是为我从数据库加载的每个事件添加一个自定义属性。 I searched around for a good while and found the addAttr function among other functions.我搜索了一段时间,在其他函数中找到了 addAttr 函数。 this function works perfectly fine when adding and saving a new event, but apparently it doesn't work when using it on the events that I loaded from the database.这个函数在添加和保存新事件时工作得很好,但显然在我从数据库加载的事件上使用它时它不起作用。 I've tried to add it before creating the scheduler and after creating the scheduler(by using getEvents and then modifying that data and using resetEvents to update them) but that didnt work either..我尝试在创建调度程序之前和创建调度程序之后添加它(通过使用 getEvents 然后修改该数据并使用 resetEvents 来更新它们),但这也不起作用..

function Schedule( boundingbox, events )
{
    var self = this;
    this.schedule = null;
    this.events = events || [];
    this.boundingbox = boundingbox || "";

    console.log( self.events );

    YUI({ lang: 'nl-NL' }).use(
        'aui-scheduler',
        function (Y) {
            var events = self.events;

            var dayView = new Y.SchedulerDayView({ isoTime: true });
            var weekView = new Y.SchedulerWeekView({ isoTime: true });
            var monthView = new Y.SchedulerMonthView({ isoTime: true });

            var eventRecorder = new Y.SchedulerEventRecorder({
                on: {
                    save: function (event) {
                        var instant = this;
                        $.ajax({
                            type: "POST",
                            url: "/EventHandler",
                            data: { "content": this.getContentNode().val(), "startDate": new Date( this.get('startDate') ).getTime() / 1000, "endDate": new Date( this.get('endDate') ).getTime() / 1000 },
                            datatype: "json",
                            success: function (output) {
                                output = JSON.parse($.trim(output));
                                instant.addAttr("eventid", {
                                   value: output.data.EV_ID,

                                   setter: function(val) {
                                       return output.data.EV_ID;
                                   },

                                   validator: function(val) {
                                       return Y.Lang.isNumber(val);
                                   }
                                });
                            }
                        });
                    },
                    edit: function (event) {
                        var instance = this;
                        console.log(instance.getAttrs()); // returns a list of attributes, eventid is not among them
                        console.log(instance.get("eventid")); // returns undefined
                        self.schedule.getEvents()[0].get( "eventid" ) // returns undefined
                    },
                    delete: function( event ) {

                    }
                }
            });

            self.schedule = new Y.Scheduler({
                strings: { agenda: 'Agenda', day: 'Dag', month: 'Maand', today: 'Vandaag', week: 'Week', year: 'Jaar',  'description-hint': "Een nieuwe afspraak" },
                activeView: weekView,
                boundingBox: self.boundingbox,
                date: new Date(),
                eventRecorder: eventRecorder,
                firstDayOfWeek: 1,
                items: self.events,
                render: true,
                views: [dayView, weekView, monthView]
            });


            $.each( self.schedule.getEvents(), function( key, value ) {
                value.addAttr( "eventid", {
                   value: 123,

                   setter: function(val) {
                       return 123;
                   },

                   validator: function(val) {
                       return Y.Lang.isNumber(val);
                   }
               });
            } );

            console.log(self.schedule.getEvents() ); // returns an array of events
            console.log( self.schedule.getEvents()[0].get( "eventid" ) ); // returns the eventid: 123
        }
    );
}

As you can see I've been trying to debug the values to see if it works, and for some reason when I log the event id right after I updated them it returns the correct eventid, but when I do it in the on-edit it returns undefined.正如您所看到的,我一直在尝试调试这些值以查看它是否有效,并且出于某种原因,当我在更新它们后立即记录事件 ID 时,它返回正确的 eventid,但是当我在 on-edit 中执行此操作时它返回未定义。

Does anybody know what I'm doing wrong here?有谁知道我在这里做错了什么?

To target the ScheduleEvent newly created, you need to store event.newSchedulerEvent in the save function.要针对新创建的ScheduleEvent ,您需要在 save 函数中存储event.newSchedulerEvent

Then you'll be able to :然后你将能够:

newSchedulerEvent.set("eventid", output.data.EV_ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM