未捕获的TypeError:undefined不是函数Backbone Template

时间:2014-04-29 01:47:13

标签: javascript jquery templates backbone.js underscore.js

即使我使用下划线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, _);
    }

1 个答案:

答案 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)