即使我使用下划线Uncaught TypeError: undefined is not a function
@template
,我的@template
上也会_.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
。当我在console.log @template
偶数内拨打initialize
时,我会
function (data) {
return render.call(this, data, _);
}
答案 0 :(得分:1)
调用context
时未指定each
参数:
@collection.each (post) ->
console.log @template( post.attributes )
所以当你说@
时,window
可能是@tempate(post.attributes)
。在致电context
时指定所需的each
:
@collection.each (post) ->
console.log @template(post.attributes)
, @
或使用带有回调的fat-arrow (=>
):
@collection.each (post) =>
console.log @template(post.attributes)