我在Marionette 2.4.1中创建CollectionView实例时遇到了问题。这是一个非常简单的例子。不知道发生了什么。看起来像木偶虫。
var AppointmentModel = Backbone.Model.extend({
});
var AppointmentCollection = Backbone.Collection.extend({
model: AppointmentModel
});
var TimeSlotView = Marionette.ItemView.extend({
});
var AppointmentCollectionView = Marionette.CollectionView.extend({
childView: TimeSlotView
});
以下是模板:
<script id="time-slot-template" type="text/template">
<div class="col-xs-4">
<button class="btn-time-slot" data-appointment-time="<%= date %>">
<%= date %>
</button>
</div>
</script>
主要代码:
//...inside ajax success
// result looks like this:
// [ "2015-1-2", "2015-1-17", "2015-2-1", "2015-2-8" ]
var appointmentCollection = new AppointmentCollection(result.map(function(apt){
return new AppointmentModel({ date: apt });
})); //browser console shows a good valid collection
var appointmentCollectionView = new AppointmentCollectionView({ collection: appointmentCollection });
// Undefined is not a function
我做错了什么?
答案 0 :(得分:1)
看起来您发布的代码没有任何问题。
可以确认它对我有效...... http://jsfiddle.net/nuewwdmr/
我做的唯一更改是在模型中加入idAttribute
,但这不会影响您遇到的这个问题。
var AppointmentModel = Backbone.Model.extend({
idAttribute: 'date'
});
我唯一能想到的是Marionette依赖于Babysitter和Wreqr(以及下划线,显然是Backbone)。你有这些依赖项吗?