我可以覆盖node_modules文件中定义的方法吗?

时间:2018-11-18 11:37:24

标签: reactjs meteor node-modules syncfusion

我正在使用sync Fusion的React计划来使用Meteor / React构建计划程序应用程序。

在我的流星应用程序的client / components文件夹中,有一个文件“ schedule.js”。

它具有以下代码:

function onEventRendered(args) {
    categoryColor=args.data.Teacher;
  console.log(args.data, );
  if (!args.element || !categoryColor) {
    return;
  }
  if (this.currentView === 'Agenda') {
    (args.element.firstChild).style.borderLeftColor = categoryColor;
  } else {
    args.element.style.backgroundColor = categoryColor;
  }
}

无论何时onEventRendered触发器,它都会自动调用位于node_modules / ej2-schedule / src / schedule / actions / crud.js中的方法之一

Crud.prototype.addEvent = function (eventData) {
        var fields = this.parent.eventFields;
        var promise = null;
        var editParms = { addedRecords: [], changedRecords: [], deletedRecords: [] };
        var args = {
            cancel: false,
            data: (eventData instanceof Array) ? eventData : [eventData],
            requestType: 'eventCreate'
        };
        this.parent.trigger(events.actionBegin, args);
        if (args.cancel) {
            return;
        }
        if (eventData instanceof Array) {
            for (var _i = 0, _a = eventData; _i < _a.length; _i++) {
                var event_1 = _a[_i];
                this.processCrudTimezone(event_1);
                editParms.addedRecords.push(event_1);
            }
            promise =
                this.parent.dataModule.dataManager.saveChanges(editParms, fields.id, this.getTable(), this.getQuery());
        }
        else {
            this.processCrudTimezone(eventData);
            promise = this.parent.dataModule.dataManager.insert(eventData, this.getTable(), this.getQuery());
        }
        var crudArgs = { requestType: 'eventCreated', cancel: false, data: eventData, promise: promise };
        this.refreshData(crudArgs);
    };

我只想添加一行以在此方法中调用流星方法“ event.add”,以便可以将数据保存在数据库中。如何实现?

1 个答案:

答案 0 :(得分:1)

我们可以在应用程序级别使用MongoDB执行CRUD,而无需修改node_modules中的任何源文件。我们准备了一个样本供您参考,可以从以下位置下载。 http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample1414642222

在上面的示例中,我们在server.js中添加了CRUD操作代码段。

app.post("/GetData", (req, res) => {  //executes on initial loading and for each CRUD actions
    dbo.collection('ScheduleData').find({}).toArray((err, cus) => {
        res.send(cus);
    });
});
app.post("/BatchData", (req, res) => {
    var eventData = [];
    if (req.body.action == "insert" || (req.body.action == "batch" && req.body.added.length > 0)) {  //this block will execute while adding events
        (req.body.action == "insert") ? eventData.push(req.body.value) : eventData = req.body.added;
        for (var i = 0; i < eventData.length; i++) {
            var sdate = new Date(eventData[i].StartTime);
            var edate = new Date(eventData[i].EndTime);
            eventData[i].StartTime = (new Date(+sdate - (sdate.getTimezoneOffset() * 60000)));
            eventData[i].EndTime = (new Date(+edate - (edate.getTimezoneOffset() * 60000)));
            dbo.collection('ScheduleData').insertOne(eventData[i]); //to add the events in MongoDB collection
        }
    }
    if (req.body.action == "update" || (req.body.action == "batch" && req.body.changed.length > 0)) { //this block will execute while editing events
        (req.body.action == "update") ? eventData.push(req.body.value) : eventData = req.body.changed;
        for (var i = 0; i < eventData.length; i++) {
            delete eventData[i]._id;
            var sdate = new Date(eventData[i].StartTime);
            var edate = new Date(eventData[i].EndTime);
            eventData[i].StartTime = (new Date(+sdate - (sdate.getTimezoneOffset() * 60000)));
            eventData[i].EndTime = (new Date(+edate - (edate.getTimezoneOffset() * 60000)));
            dbo.collection('ScheduleData').updateOne({ "Id": eventData[i].Id }, eventData[i]); //to update the events in MongoDB collection
        }
    }
    if (req.body.action == "remove" || (req.body.action == "batch" && req.body.deleted.length > 0)) { //this block will execute while deleting events
        (req.body.action == "remove") ? eventData.push(req.body.value) : eventData = req.body.deleted;
        for (var i = 0; i < eventData.length; i++) {
            dbo.collection('ScheduleData').deleteOne({ "Id": eventData[i].Id }, eventData[i]); //to delete the events in MongoDB collection
        }
    }
    res.send(req.body);
});

在下面的代码中,我们将GetData和BatchData url路径提供给初始提取以及使用UrlAdaptor执行CRUD操作并将其分配给调度程序数据源。

let data = new DataManager({url:'http://localhost:5000/GetData',crudUrl:'http://localhost:5000/BatchData',适配器:new UrlAdaptor,crossDomain:true});

eventSettings = {{dataSource:data}}

运行示例的步骤:

  • 运行MongoDB并在“ mydb”数据库中创建名为“ ScheduleData”的集合。
  • 运行以下评论

  • npm install

  • npm运行服务器
  • npm开始