全部,
我正在使用 Backbone JS 构建一个应用程序,并且根据我的方法,如果第一个模型查找太旧,我会向服务器发出请求以更新每个 hashchange 上的本地存储。
所以我正在处理的是这个。
(hashchange) ->
fetch model ->
(if model expired [eg: grabbed from LS], fetch again in background)
render
在 View 中,我已将更改事件绑定(bind)到指向渲染的模型,这样当第二个请求返回时,页面就会重新渲染。
对于各位绅士和女士们,我问为什么当第一页加载时所有事件都起作用(点击等),但是如果 View 重新渲染,它们就不再起作用了?
一些代码可以帮助您查看 View ,有一个带有嵌套请求的嵌套 View :
class ShowView extends Backbone.View
template: +some template+
initialize: ->
this.render()
@model.bind('change', this.render)
render: ->
$(@el).html(this.template(@model.toJSON())
items = new ItemCollection
@model.set(items: items, {silent: true})
@$items = new ItemsView(collection: items, el: @$('#posts'))
items.fetch()
+displayPage @el+
@
class ItemsView extends Backbone.View
template: +some template+
initialize: ->
@collection.bind('reset', this.render)
render: =>
@collection.each( (model) -> new ItemView(model: model))
@
......我发誓还有一个......
class ItemView extends Backbone.View
template: +some template+
events:
'click .inner': 'showItem'
initialize: ->
this.render()
render: ->
$(@el).html(this.template(@model.toJSON()))
@
所有这些都被概括了,但概念是相同的。
当该更改事件触发并且所有内容都被刷新并且我单击 .inner 时,什么也没有发生。
建议?我试图在新的渲染发生之前取消绑定(bind)和删除 View ,我也尝试在所有渲染方法的末尾委托(delegate)事件。
谢谢
- 更新 -
ItemView 模板:
<a class="inner">
<img src="item/image.gif" />
<h3><%= title %></h3>
<p><%= description %></p>
</a>
ItemView 中的 showItem 方法只是对实际项目的简单重定向:
showItem: ->
window.location.hash = "#/posts/#{@model.id}/show"
我忘了提到 ItemView 的 tagName 是 'li' 而 ItemsView tagName 是 'ul',所以这一切只是在页面上列出一个列表。
最佳答案
关于backbone.js - Backbone js 重新渲染使 View 无事发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7640865/