Uncaught TypeError: undefined не является функцией Backbone Template

0

Я получаю 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, _);
    }
Теги:
templates
underscore.js
backbone.js

1 ответ

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)
, @

или используйте жир-стрелку (=>) с обратным вызовом:

@collection.each (post) =>
  console.log @template(post.attributes)
  • 0
    Да, я только нашел ответ к тому времени, когда я оглянулся на этот вопрос.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню