javascript 类型错误:在 Meteor 应用程序中使用 tsega:bootstrap3-datetimepicker 时无法读取未定义的属性“日期”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34176963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: Cannot read property 'date' of undefined when using tsega:bootstrap3-datetimepicker in Meteor app
提问by Cengiz
I have set up a meteor app that uses tsega:bootstrap3-datetimepicker for dates.
我已经设置了一个使用 tsega:bootstrap3-datetimepicker 作为日期的流星应用程序。
I have the following in detail.js:
我在detail.js 中有以下内容:
Template.detail.helpers({
editbuttonstate: function () {
return (Session.get('editing')) ? 'glyphicon-ok' : 'glyphicon-pencil';
},
formattedStartDate: function () {
return moment(this.startDate).format('DD/MM/YYYY HH:mm');
},
formattedEndDate: function () {
return moment(this.endDate).format('DD/MM/YYYY HH:mm');
}
});
Template.detail.events({
'click .toggle-edit': function (e) {
e.preventDefault();
var editflag = Session.equals('editing', true);
if (!editflag) {
initDateTimePickers();
} else if (Meteor.userId()) {
var updatedEvent = {};
updatedEvent.startDate = $('#input-start-date').data('DateTimePicker').date().toDate();
updatedEvent.endDate = $('#input-end-date').data('DateTimePicker').date().toDate();
updatedEvent.owner = Meteor.userId();
Events.upsert({
_id: this._id
}, {
$set: updatedEvent
});
}
if (!editflag && !Meteor.userId()) {
return;
}
Session.set('editing', (!editflag));
}
});
Template.detail.rendered = function() {
initDateTimePickers();
};
var initDateTimePickers = function() {
var now = new Date();
$('.datetimepicker').datetimepicker({
locale: 'en_gb',
format: 'DD/MM/YYYY HH:mm',
defaultDate: new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 12, 0, 0)
});
$('.set-due-date').focus(function () {
$('.datetimepicker').data("DateTimePicker").show();
});
$("#input-start-date").on("dp.change", function (e) {
$('#input-end-date').data("DateTimePicker").minDate(e.date);
});
$("#input-end-date").on("dp.change", function (e) {
$('#input-start-date').data("DateTimePicker").maxDate(e.date);
});
};
...and the following detail.html:
...以及以下detail.html:
<template name="detail">
<div class="page page-detail">
<div class="container">
{{#if editing}}
<div class="input-group datetimepicker" id="input-start-date">
<span class="input-group-addon" id="addon-quote">Start</span>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
<input class="set-due-date form-control" type="text" value="{{formattedStartDate}}" />
</div>
<div class="input-group datetimepicker" id="input-end-date">
<span class="input-group-addon" id="addon-quote">End</span>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
<input class="set-due-date form-control" type="text" value="{{formattedEndDate}}" />
</div>
{{else}}
<h4 class="title">Start: {{formattedStartDate}}</h4>
<h4 class="title">End: {{formattedEndDate}}</h4>
{{/if}}
</div>
<div class="container">
<div class="btn btn-danger toggle-edit pull-right">
<span class="glyphicon {{editbuttonstate}}"></span>
</div>
</div>
</div>
</template>
When the template is first rendered, the date-time pickers work fine, but when save button is clicked followed by an edit button click, the date-time pickers are not functional anymore.
当第一次呈现模板时,日期时间选择器工作正常,但是当单击保存按钮然后单击编辑按钮时,日期时间选择器不再起作用。
Upon save, they produce errors like:
保存后,它们会产生如下错误:
TypeError: Cannot read property 'date' of undefined
类型错误:无法读取未定义的属性“日期”
Anyone have any idea what could be missing?
任何人都知道可能会丢失什么?
采纳答案by Cengiz
It looks like every time the edit/save button is clicked, new HTML elements are generated for the display/edit mode including the elements that make up the date-time pickers. Therefore, the date-time pickers have to be reinitialized every time edit mode is toggled.
看起来每次单击编辑/保存按钮时,都会为显示/编辑模式生成新的 HTML 元素,包括构成日期时间选择器的元素。因此,每次切换编辑模式时都必须重新初始化日期时间选择器。
One way to achieve this is to separate the core of the edit view into a separate template as follows.
实现此目的的一种方法是将编辑视图的核心分离到一个单独的模板中,如下所示。
detail.js
细节.js
Template.detail.helpers({
editbuttonstate: function () {
return (Session.get('editing')) ? 'glyphicon-ok' : 'glyphicon-pencil';
},
editing: function () {
return (Session.equals('editing', true));
},
formattedStartDate: function () {
return moment(this.startDate).format('DD/MM/YYYY HH:mm');
},
formattedEndDate: function () {
return moment(this.endDate).format('DD/MM/YYYY HH:mm');
}
});
Template.edit_detail.helpers({
gameActive: function () {
return this.type === "game" ? "active" : "";
},
tournamentActive: function () {
return this.type === "tournament" ? "active" : "";
},
trainingActive: function () {
return this.type === "training" ? "active" : "";
},
campActive: function () {
return this.type === "camp" ? "active" : "";
},
formattedStartDate: function () {
return moment(this.startDate).format('DD/MM/YYYY HH:mm');
},
formattedEndDate: function () {
return moment(this.endDate).format('DD/MM/YYYY HH:mm');
}
});
Template.detail.events({
'click .toggle-edit': function (e) {
e.preventDefault();
var editflag = Session.equals('editing', true);
if (editflag && Meteor.userId()) {
var updatedEvent = {};
updatedEvent.startDate = $('#input-start-date').data("DateTimePicker").date().toDate();
updatedEvent.endDate = $('#input-end-date').data("DateTimePicker").date().toDate();
updatedEvent.owner = Meteor.userId();
Events.upsert({
_id: this._id
}, {
$set: updatedEvent
});
}
if (!editflag && !Meteor.userId()) {
return;
}
Session.set('editing', (!editflag));
}
});
Template.edit_detail.rendered = function() {
initDateTimePickers();
};
var initDateTimePickers = function() {
if (Session.equals('editing', true)) {
var now = new Date();
$('.datetimepicker').datetimepicker({
locale: 'en_gb',
format: 'DD/MM/YYYY HH:mm',
defaultDate: new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 12, 0, 0)
});
$('#input-end-date').data("DateTimePicker").minDate(now);
$('#input-start-date').data("DateTimePicker").maxDate(now);
$("#input-start-date").on("dp.change", function (e) {
$('#input-end-date').data("DateTimePicker").minDate(e.date);
});
$("#input-end-date").on("dp.change", function (e) {
$('#input-start-date').data("DateTimePicker").maxDate(e.date);
});
}
};
detail.html
详细信息.html
<template name="detail">
<div class="page page-detail">
<div class="container">
{{#if editing}}
{{> edit_detail}}
{{else}}
<h4 class="title">Start: {{formattedStartDate}}</h4>
<h4 class="title">End: {{formattedEndDate}}</h4>
{{/if}}
</div>
<div class="container">
<div class="btn btn-danger toggle-edit pull-right">
<span class="glyphicon {{editbuttonstate}}"></span>
</div>
</div>
</div>
</template>
<template name="edit_detail">
<div class="input-group datetimepicker" id="input-start-date">
<span class="input-group-addon" id="addon-quote">Start</span>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
<input class="set-due-date form-control" type="text" value="{{formattedStartDate}}" />
</div>
<div class="input-group datetimepicker" id="input-end-date">
<span class="input-group-addon" id="addon-quote">End</span>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
<input class="set-due-date form-control" type="text" value="{{formattedEndDate}}" />
</div>
</template>
Moving the initialization code for the date-time pickers to run when the new template is rendered solves the problem.
移动日期时间选择器的初始化代码以在呈现新模板时运行解决了该问题。