调用视图中的骨干模型函数
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
我用的是coffescript
您需要在视图中初始化模型,方法如下: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)
相关文章:
- 如何在视图模型contet更新更新上调用Jquery函数
- Angular,函数在(模型)工厂中返回值
- 将模型从Kendo Mvc UI网格传递到javascript函数
- 模型不是构造函数主干
- 挖空视图模型函数仅影响最后一个实例
- 主干模型:初始化与构造函数
- KnockoutJS:如何避免在applyBindings上运行视图模型函数
- 在IE9中未检测到视图模型函数绑定,但在FF和Chrome中有效
- 在挖空JS中从ATTR调用模型函数以获取动态属性
- 如何在 Laravel 4 中将模型函数从控制器导出到视图
- jQuery UI的“可选” - 需要调用Codeigniter模型函数
- 在范围之外调用 AJAX 成功的淘汰模型函数
- Ember&RSVP:如何解决路由中的嵌套关系's模型函数
- 如何限制angularjs中对模型函数的调用频率
- 如何找到两个输入的模型函数
- 函数未定义,Bookshelf.js模型函数未被识别为函数
- js相关代码中的视图模型函数名问题
- Ember.js模型函数,无法读取属性'的定义
- SailsJs,我怎么能让一个模型函数成为一个承诺,这样我就可以在控制器中排队
- 调用视图中的骨干模型函数