为什么 Backbone 视图将其模型称为“add”?在绑定“全部”期间渲染方法? (这是骨干+node.js实现)
所以我已经这样好几天了。我正在开发应用程序的聊天组件。所有通信都工作正常,聊天记录使用 Redis 保存,Backbone 控制器功能正常。问题是在前端渲染聊天条目。这是有问题的代码:
class views.ChatItemView extends Backbone.View
tagName: 'li',
initialize: (options) ->
console.log(@.model)
_.bindAll @, 'render'
@model.bind 'all', @render
render: ->
$(@el).html @model.get('name') + ': ' + @model.get 'text'
@
这是错误的 Firebug 警报:
Uncaught TypeError: Object add has no method 'bind'
views.ChatItemView.ChatItemView.initializeviews.js:21
Backbone.Viewbackbone.js:884
ChatItemViewviews.js:13
views.ChatView.ChatView.addChatviews.js:76
Backbone.Events.triggerbackbone.js:117
_.extend._onModelEventbackbone.js:635
Backbone.Events.triggerbackbone.js:117
_.extend._addbackbone.js:595
_.extend.addbackbone.js:451
views.ChatView.ChatView.msgReceivedviews.js:90
NodeChatController.initcontroller.js:17
EventEmitter.emitsocket.io.js:627
SocketNamespace.onPacketsocket.io.js:2171
Socket.onPacketsocket.io.js:1861
Transport.onPacketsocket.io.js:1309
Transport.onDatasocket.io.js:1286
WS.open.websocket.onmessage
有人知道为什么会发生这种情况吗?或者有人遇到类似的问题吗?
So I've been having this for quite a few days. I'm working on the chat component of the application. All the communication works fine, chats are saved with Redis, Backbone controllers function just fine. The problem is rendering the chat entries in the front end. This is the problematic code:
class views.ChatItemView extends Backbone.View
tagName: 'li',
initialize: (options) ->
console.log(@.model)
_.bindAll @, 'render'
@model.bind 'all', @render
render: ->
$(@el).html @model.get('name') + ': ' + @model.get 'text'
@
This is the Firebug alert for error:
Uncaught TypeError: Object add has no method 'bind'
views.ChatItemView.ChatItemView.initializeviews.js:21
Backbone.Viewbackbone.js:884
ChatItemViewviews.js:13
views.ChatView.ChatView.addChatviews.js:76
Backbone.Events.triggerbackbone.js:117
_.extend._onModelEventbackbone.js:635
Backbone.Events.triggerbackbone.js:117
_.extend._addbackbone.js:595
_.extend.addbackbone.js:451
views.ChatView.ChatView.msgReceivedviews.js:90
NodeChatController.initcontroller.js:17
EventEmitter.emitsocket.io.js:627
SocketNamespace.onPacketsocket.io.js:2171
Socket.onPacketsocket.io.js:1861
Transport.onPacketsocket.io.js:1309
Transport.onDatasocket.io.js:1286
WS.open.websocket.onmessage
Does anyone have any idea why this happens or did anyone have similar problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定为什么会出现错误,但是,使用 CoffeeScript 时不需要 _.bindAll。只需使用粗箭头作为渲染函数:
Not sure why you're getting your error, but, you don't need to _.bindAll when using CoffeeScript. Just use the fat arrow for the render function:
我想通了。
这是 chatView 初始化代码,
它将“add”绑定到 @addChat 操作。
这种方式工作得很好,但以前我有:
所以,将“all”绑定到@addChat 导致了麻烦。
I figured it out.
This is the chatView initialization code
It binds 'add' to the @addChat action.
It works just fine this way but previously I had:
So, binding 'all' to @addChat was causing the trouble.
我对此不确定,因为咖啡脚本不是我的事,
但
console.log(@.model)
似乎有效?但是当您尝试绑定方法时使用@model
这不也应该是
i'm not sure about this since coffeescript aint my thing,
but
the
console.log(@.model)
seems to work? but you use@model
when you try the bind methodshouldn't this also be