.:编辑:. 如果您遇到同样的问题,请检查您的变量初始化,我忘记了new。
我不断收到 TypeError: this._ensureElement is not a function 并在早些时候收到 TypeError: this._reset is not a function 但我无法真正重新创建后者的精确设置。
我的脚本顺序正确:
<script src="js/components/jquery.js"></script>
<script src="js/components/underscore.js"></script>
<script src="js/components/backbone.js"></script>
<script src="js/script.js"></script>
我的模型是在我的收藏之前注册的。
这是一个购物车应用程序。
#namespacing
App =
Collection : {}
Model : {}
View : {}
###
MODEL
###
class ModelItem extends Backbone.Model
#default values
defaults:
name : 'Product Name'
quantity : 0
unit : 'kg'
#Increase or decrease the quantity
change_quantity : (type) ->
qty = @get 'quantity'
@set 'quantity', if type is 'increase' then ++qty else --qty
###
COLLECTION
###
class CollectionItems extends Backbone.Collection
model: ModelItem
###
VIEW
###
class ViewItems extends Backbone.View
window.view = ViewItems()
window.item = ModelItem()
最佳答案
您缺少 new
运算符。
window.view = new ViewItems()
window.item = new ModelItem()
关于javascript - Coffeescript 和 Backbone.js - TypeError : this. _ensureElement 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227801/