刚刚开始使用骨干网,我想到我熟悉有关数据绑定的所有警告。事实证明我不是。如果有人有任何想法,为什么以下集合没有绑定到IndexView我会非常感激。 (在浏览器和chrome控制台中手动尝试过...未触发的console.log调用确认render()未被触发)
从控制台,尝试呼叫是:
window.router.project_view.options.projects.create({name: 'TestProject'})
项目已成功创建,但未调用render()
class Backbonescaffolddemo.Routers.CalendarsRouter extends Backbone.Router
routes:
"" : "index"
index: () ->
@projects = new Backbonescaffolddemo.Collections.ProjectsCollection()
...
@project_view = new Backbonescaffolddemo.Views.Projects.IndexView(projects: @projects)
@task_view = new Backbonescaffolddemo.Views.Tasks.IndexView(tasks: @tasks)
...
$('#projects').html(@project_view.render().el)
class Backbonescaffolddemo.Models.Project extends Backbone.RelationalModel
paramRoot: 'project'
class Backbonescaffolddemo.Collections.ProjectsCollection extends Backbone.Collection
model: Backbonescaffolddemo.Models.Project
url: '/projects'
Backbonescaffolddemo.Views.Projects ||= {}
class Backbonescaffolddemo.Views.Projects.IndexView extends Backbone.View
template: JST["backbone/templates/projects/index"]
id: "projects"
initialize: () ->
@options.projects.fetch()
_.bindAll(this, 'addOne', 'addAll', 'render')
@options.projects.bind('reset', @addAll)
@options.projects.bind('save', @addAll)
@options.projects.bind('change', @addAll)
@options.projects.bind('change', @render)
@options.projects.bind('create', @render)
addAll: () ->
@options.projects.each(@addOne)
addOne: (project) ->
view = new Backbonescaffolddemo.Views.Projects.ProjectView({model: project})
$(@el).append(view.render().el)
render: ->
console.log 'render called'
$(@el).html(@template(projects: @options.projects.toJSON()))
return this
答案 0 :(得分:2)
通过create
或任何其他方式将模型添加到集合中会触发该集合上的add
事件,而不是create
事件。变化
@options.projects.bind('create', @render)
到
@options.projects.bind('add', @render)