gpt4 book ai didi

javascript - 是否可以只重新渲染模板的一部分

转载 作者:行者123 更新时间:2023-12-03 11:34:48 25 4
gpt4 key购买 nike

我有一个包含 div #entries#movies 的模板 在我的 #entries div 中,我显示了我的电影收藏的模型。在我的 #movies div 中,我显示了搜索查询的结果。 (元素的命名相当糟糕,我同意)。

在呈现模板的 View 中,我有以下事件,

  events: -> 
"click li": "addEntry"
"click .remove": "destroyEntry"

addEntry: (e) ->
movie_title = $(e.target).text()
@collection.create title: movie_title

appendEntry: (entry) ->
view = new Movieseat.Views.Entry(model: entry)
$('#entries').append(view.render().el)

destroyEntry: (e) ->
thisid = @$(e.currentTarget).closest('div').data('id')
@collection.get(thisid).destroy()

li 元素在 #movies div 中呈现。这样我就可以单击不同 View 中的结果并将它们添加到其他集合中。这可行,但有一个问题。

在同一个 View 中我有这个,

  initialize: -> 
@collection.on('change', @render, this)
@collection.on('add', @appendEntry, this)
@collection.on('destroy', @render, this)
return

问题是,当集合发生更改时(例如当我向其中添加或删除电影时),它会重新呈现模板。这意味着它会重新呈现 #entries#movies div。但现在它只渲染一个空的 #movies div(没有 searchquery 集合)。

所以我想知道是否可以只渲染模板的一部分。在这种情况下,我只想重新渲染 #entries div 并保留 #movies div。

最佳答案

你可以试试这个:

@collection.on('change', @renderEntries, this)

renderEntries: (entry) ->
$('#entries').html(render('entries', { entries: yourEntriesCollection.toJSON() }))

//some common.js file

function render(template_name, data)
{
//here create logic to render specified template by name with data.
// see example here: http://stackoverflow.com/questions/8366733/external-template-in-underscore
return html;
}

您应该为可以单独呈现的条目创建模板。

关于javascript - 是否可以只重新渲染模板的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26563847/

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