[英]Call backbone model function in view
我的模型中有一個簡單函數,如果優先級為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
就像我使用咖啡的音符一樣
您需要通過以下兩種方法之一在視圖中初始化模型: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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.