gpt4 book ai didi

javascript - Chaplin/Backbone 问题 - 将项目添加到集合时未触发 "Add"事件

转载 作者:行者123 更新时间:2023-11-30 17:54:09 25 4
gpt4 key购买 nike

我正在为自己构建一个测试应用程序,以了解有关 coffeescript、Backbone、Brunch.io 和 Chaplin JS 的更多信息,但我被卡住了,无法弄清楚自己做错了什么。

这是我在 todo-view.coffee 中的代码:

View = require 'views/base/view'
TodoItemView = require 'views/todo/todo-item'
TodoItemModel = require 'models/todo/todo-item-model'
TodoItemCollection = require 'models/todo/todo-collection'

# Site view is a top-level view which is bound to body.
module.exports = class TodoView extends View

# Template data stuff
container: 'todo-container'
tagName: 'ul'
className: 'todo-list'
template: require './templates/todo'

# Create a custom initialize method
initialize: ->
super

# Create a new Backbone Collection with some dummy data to store
@collection = new TodoItemCollection()

# Store the dummy data in the collection
data = ["working", "studying", "gym", "sleep"]
for todoItem in data
@collection.add( new TodoItemModel({ item: todoItem }) )

# Render the view
@render()

# Listen to data events
listen:
"add collection": "renderTodoList"

# When the template is initialized we need to load
# all the list items and append them to the container
renderTodoList: ->

# Loop over all the items
for model in @collection.models
todoItemView = new TodoItemView({ container: @$el, model: model })

问题是:事件监听器(在监听器对象中设置)没有被触发。所以 @renderTodoList 没有被调用。

直接从@initialize 函数调用@renderTodoList 确实有效。但我希望该函数由集合上的“添加”事件触发。

我还尝试通过在创建新数据模型的循环中添加 @collection.trigger "add"来手动触发事件。但这也不起作用。

有什么想法是我监督的或做错了什么吗?

最佳答案

斯特凡,

当我尝试对事件使用监听哈希时,我遇到了类似的问题。我选择在 View 的初始化方法中设置监听器。

@listenTo @Collection, '添加', @renderTodoList, @

-汉斯

关于javascript - Chaplin/Backbone 问题 - 将项目添加到集合时未触发 "Add"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18410182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com