gpt4 book ai didi

javascript - Marionette.CompositeView 中的 templateHelpers

转载 作者:行者123 更新时间:2023-11-29 18:27:25 26 4
gpt4 key购买 nike

我不知道为什么这段代码不起作用。
阅读documentation ,
应该调用 templateHelpers

我的目标是将 this.collection.length 传递给模板。

有什么提示吗?谢谢。

我正在使用 Backbone.Marionette v0.9.5


return Marionette.CompositeView.extend({
className: 'user-board',

template: usersTemplate,

itemView: userItemView,

initialize: function () {
this.collection = new UseList();
this.collection.fetch();
},

appendHtml: function (collectionView, itemView) {
collectionView.$el.find('ul.users-list').append(itemView.el);
},

templateHelpers: function () {
console.log(this.collection.length);
},

serializeData: function () {
return {
weekly: this.options.weekly,
users_length: this.collection.length // here the length is zero
// after the fetch the length is > 0
// but in template remains 0
};
}
});

要解决我的问题,我必须执行以下操作......

    initialize: function () {
_.bindAll(this, 'render');
this.collection = new NewCollection();
this.collection.fetch({
success: this.render
});
}

有没有更好的方法让它发挥作用?

最佳答案

阅读Marionette Documentation serializeData 方法,即使用 mixinTemplateHelpers 的方法,仅在 Item View.render 方法上调用 here在您当前的代码中,您根本不呈现

更新:这样,每次集合接收到新数据时,它都会将您的 View 更新为新长度

initialize: function () {
_.bindAll(this, 'render');
this.collection = new NewCollection();
this.collection.fetch();
this.collection.bind('reset', this.render);
}

关于javascript - Marionette.CompositeView 中的 templateHelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690406/

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