我buidling与骨干JS的应用程序,并按照我的方法,我烧一个请求到服务器来更新每个hashchange我的本地存储,如果第一个模型查找太旧了。
所以我正在处理的是这个。
(hashchange) ->
fetch model ->
(if model expired [eg: grabbed from LS], fetch again in background)
render
在我已绑定change事件对模型指向渲染,这样当第二次请求回来的页面视图只是重新呈现。
对于你们的男性和女性,我问为什么当第一页加载所有的事件工作(点击等),但如果视图重新呈现,他们不再?
一些代码来帮助你的意见,有一个嵌套的请求嵌套的观点:
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,没有任何反应。
对此提出建议?我试图在新渲染发生之前解除绑定并删除视图,我也试图在所有渲染方法的末尾委派事件。
感谢
--UPDATE--
的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',所以这一切都是在页面上列出一个列表。
ItemView的模板是什么样的?另外,ItemView的'showItem'方法看起来像什么? –
你可以创建一个jsFiddle吗? –
我正在为它的jsFiddle工作,但帖子已更新。 –