javascript - Call backbone model function in view -


i have simple function in model should return true if priority 100

class app.models.publication extends backbone.model   urlroot: '/api/publications'    isincredible: ->     @get('priority') 100 

in view wanna call function, can't

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()     

i get: typeerror: this.model.isincredible not function

just note i'm using coffescript

you need initialize model in view either 1) setting in initialize function of view

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()     

or 2) pass in model instance argument view when instanciating it

pubmodel = new app.models.publication(/*...*/) pubshow = new app.views.publicationshow(model: pubmodel) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -