我的模型中有一个简单的函数,如果优先级为100
,则返回trueclass 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
就像我正在使用coffescript
一样答案 0 :(得分: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)