为什么 Backbone 视图将其模型称为“add”?在绑定“全部”期间渲染方法? (这是骨干+node.js实现)

发布于 2024-12-26 06:27:46 字数 1172 浏览 0 评论 0原文

所以我已经这样好几天了。我正在开发应用程序的聊天组件。所有通信都工作正常,聊天记录使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单爱 2025-01-02 06:27:46

不确定为什么会出现错误,但是,使用 CoffeeScript 时不需要 _.bindAll。只需使用粗箭头作为渲染函数:

class views.ChatItemView extends Backbone.View
    tagName: 'li',

    initialize: (options) ->
        console.log(@.model)
        @model.bind 'all', @render

    render: =>
        $(@el).html @model.get('name') + ': ' + @model.get 'text'
        @

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:

class views.ChatItemView extends Backbone.View
    tagName: 'li',

    initialize: (options) ->
        console.log(@.model)
        @model.bind 'all', @render

    render: =>
        $(@el).html @model.get('name') + ': ' + @model.get 'text'
        @
一抹苦笑 2025-01-02 06:27:46

我想通了。

这是 chatView 初始化代码,

class views.ChatView extends Backbone.View
    initialize: (options) ->
        @model.chats.bind 'add', @addChat
        @socket = options.socket

    msgReceived: (message) ->
            newChatEntry = new models.ChatItem
            newChatEntry.mport message.data
            @model.chats.add newChatEntry

它将“add”绑定到 @addChat 操作。

    addChat: (chat) ->
        view = new views.ChatItemView {model: chat}
        $('#chat_list').append view.render().el

这种方式工作得很好,但以前我有:

        @model.chats.bind 'all', @addChat

所以,将“all”绑定到@addChat 导致了麻烦。

I figured it out.

This is the chatView initialization code

class views.ChatView extends Backbone.View
    initialize: (options) ->
        @model.chats.bind 'add', @addChat
        @socket = options.socket

    msgReceived: (message) ->
            newChatEntry = new models.ChatItem
            newChatEntry.mport message.data
            @model.chats.add newChatEntry

It binds 'add' to the @addChat action.

    addChat: (chat) ->
        view = new views.ChatItemView {model: chat}
        $('#chat_list').append view.render().el

It works just fine this way but previously I had:

        @model.chats.bind 'all', @addChat

So, binding 'all' to @addChat was causing the trouble.

当梦初醒 2025-01-02 06:27:46

我对此不确定,因为咖啡脚本不是我的事,

console.log(@.model) 似乎有效?但是当您尝试绑定方法时使用 @model
这不也应该是

@.model.bind 'all', @render

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 method
shouldn't this also be

@.model.bind 'all', @render
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文