【发布时间】:2014-12-09 05:07:52
【问题描述】:
当显示一个包含大约 200 个国家/地区的下拉复合视图集合时,我的应用程序变得太慢了。
在木偶复合视图中处理大型集合时提高性能的最佳方法是什么?
这是控制器中加载速度非常慢的函数。只删除以下几行就很快:
@layout.shippingCountryRegion.show shippingCountryView
@layout.billingCountryRegion.show billingCountryView
所以这似乎是一个非常缓慢的渲染问题。
Show.Controller =
showProfile: ->
@layout = @getLayoutView()
@layout.on "show", =>
headerView = @getHeaderView()
@layout.headerRegion.show headerView
accessView = @getAccessView()
@layout.accessRegion.show accessView
billingReadmeView = @getBillingReadmeView()
@layout.billingReadmeRegion.show billingReadmeView
billingFieldsView = @getBillingFieldsView()
@layout.billingFieldRegion.show billingFieldsView
shippingReadmeView = @getShippingReadmeView()
@layout.shippingReadmeRegion.show shippingReadmeView
shippingFieldsView = @getShippingFieldsView()
@layout.shippingFieldRegion.show shippingFieldsView
MyApp.request "location:get_countries", (countries) =>
billingCountryView = @getBillingCountryView(countries)
@layout.billingCountryRegion.show billingCountryView
MyApp.request "location:get_states", MyApp.activeCustomer.get('billing_country_id'), (states) =>
billingStateView = @getBillingStatesView(states)
@layout.billingStateRegion.show billingStateView
MyApp.request "location:get_countries", (countries) =>
shippingCountryView = @getShippingCountryView(countries)
@layout.shippingCountryRegion.show shippingCountryView
MyApp.request "location:get_states", MyApp.activeCustomer.get('shipping_country_id'), (states) =>
shippingStateView = @getShippingStatesView(states)
@layout.shippingStateRegion.show shippingStateView
MyApp.mainRegion.show @layout
计费国家/地区视图:
class View.BillingCountryDropdownItem extends MyApp.Views.ItemView
template: billingCountryItemTpl
tagName: "option"
onRender: ->
this.$el.attr('value', this.model.get('id'));
if MyApp.activeCustomer.get('billing_country_id') == this.model.get('id')
this.$el.attr('selected', 'selected');
class View.BillingCountryDropdown extends MyApp.Views.CompositeView
template: billingCountryTpl
itemView: View.BillingCountryDropdownItem
itemViewContainer: "select"
模板,简单地说:
<label>Country
<select id="billing_country_id" name="billing_country_id">
<%- name %>
</select>
</label>
【问题讨论】:
-
你为什么用
onRender方法设置模型的值? -
复合模板中的 是什么?
标签: backbone.js collections marionette