Я получаю Uncaught TypeError: undefined is not a function
на моем @template
хотя я определил @template
используя Underscore _.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, _);
}
Вы не указали аргумент context
при вызове each
:
@collection.each (post) ->
console.log @template( post.attributes )
поэтому @
- это, вероятно, window
когда вы говорите @tempate(post.attributes)
. Укажите желаемый context
при each
вызове:
@collection.each (post) ->
console.log @template(post.attributes)
, @
или используйте жир-стрелку (=>
) с обратным вызовом:
@collection.each (post) =>
console.log @template(post.attributes)