0

我的模型中有一个简单的函数,如果优先级为 100,它应该返回 true

class App.Models.Publication extends Backbone.Model
  urlRoot: '/api/publications'

  isIncredible: ->
    @get('priority') is 100

在视图中我想调用该函数,但我不能

class App.Views.PublicationShow extends Backbone.View
  tagName: 'article'
  className: 'offer'
  template: JST['publications/show']

  render: =>
    if @model.isIncredible()
      $(@el).addClass('incredible').html(@template(publication: @model))
    else
      $(@el).html(@template(publication: @model))
    @modalEvent()
    this

我得到:TypeError: this.model.isIncredible is not a function

就像我正在使用咖啡脚本一样

4

1 回答 1

1

您需要通过以下任一方式在视图中初始化模型 1)在视图的初始化函数中设置它

class App.Views.PublicationShow extends Backbone.View
  tagName: 'article'
  className: 'offer'
  template: JST['publications/show']

  initialize: ->
      @model = new App.Models.Publication()

  render: =>
    if @model.isIncredible()
      $(@el).addClass('incredible').html(@template(publication: @model))
    else
      $(@el).html(@template(publication: @model))
    @modalEvent()
    this

或 2) 在实例化时将模型实例作为参数传递给视图

pubModel = new App.Models.Publication(/*...*/)
pubShow = new App.Views.PublicationShow(model: pubModel)
于 2013-04-15T02:13:45.543 回答