即使我使用 Underscore 的 定义了
@template
,我的 @template
上仍然收到 Uncaught TypeError: undefined is not a function
>_.template()
IntroProject.Views.Posts ||= {}
class IntroProject.Views.Posts.IndexView extends Backbone.View
el: '#posts'
template: _.template( $('#home-post-template').html() ) if $('#home-post-template').length
initialize: ->
@collection.bind "reset", ->
@render()
, @
render: ->
@collection.each (post) ->
console.log @template( post.attributes )
@
当我执行 console.log @template
时,如果在渲染函数中调用,我会得到 undefined
。当我从 initialize
中调用 console.log @template
时,我得到了
function (data) {
return render.call(this, data, _);
}
最佳答案
调用 each
时您尚未指定 context
参数:
@collection.each (post) ->
console.log @template( post.attributes )
所以当你说@tempate(post.attributes)
时,@
可能是window
。调用 each
时指定所需的上下文
:
@collection.each (post) ->
console.log @template(post.attributes)
, @
或使用 fat-arrow (=>
)使用回调:
@collection.each (post) =>
console.log @template(post.attributes)
关于javascript - 未捕获的类型错误 : undefined is not a function Backbone Template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354114/