【问题标题】:How to render only three data elements each time when you click on load more? Backbone每次单击加载更多时如何仅渲染三个数据元素?骨干
【发布时间】:2019-08-11 22:24:37
【问题描述】:

我有一张卡片元素列表,每张卡片都包含一些信息。使用加载更多选项,我尝试一次只加载三个卡片元素,默认情况下最初只加载两个。以下是我的视图文件:

(function() {
  var __extends = function(child, parent) { for (var key in parent) { 
if (__hasProp.call(parent, key)) child[key] = parent[key]; } function 
ctor() { this.constructor = child; } ctor.prototype = parent.prototype; 
child.prototype = new ctor(); child.__super__ = parent.prototype; 
return child; },
__hasProp = {}.hasOwnProperty;

  TalentCardNew.Views.CertificationCard = (function(_super) {
__extends(CertificationCard, _super);

function CertificationCard() {
  return CertificationCard.__super__.constructor.apply(this, arguments);
}

CertificationCard.prototype.template = JST['talent_card_new/templates/common/certification_card'];

CertificationCard.prototype.className = 'certification-container';

CertificationCard.prototype.events = {
  'click .listCertificatesContainer': 'showMoreCertificates'
};

CertificationCard.prototype.initialize = function(options) {
  if (options == null) {
    options = {};
  }
  this.view_type = options.view_type;
  this.card_data = new Backbone.Collection();
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2019"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2018"
  }));
  return this.card_data.add(new Backbone.Model({
    name: "Accounting Qualification Excellence",
    place: "Accounting Institue",
    valid_till: "Dec 14, 2019"
  }));
};

CertificationCard.prototype.render = function() {
  $(this.el).html(this.template({
    view_type: this.view_type,
    card_data: this.card_data
  }));
  if (this.view_type === 'my_profile') {
    this.renderModal();
  }
  this.renderLoadMore();
  return this;
};

CertificationCard.prototype.renderModal = function() {
  var popup_view;
  popup_view = new TalentCardNew.Views.AddCertification();
  return $(this.el).find('#modalContainer').html(popup_view.render().el);
};

CertificationCard.prototype.renderLoadMore = function() {
  if ((this.view_type === 'my_profile' && this.card_data.length > 2) || (this.card_data.length > 3)) {
    return $(this.el).find('.listCertificatesContainer').removeClass('hide');
  }
};

return CertificationCard;

})(Backbone.View);

}).call(this);

我的Haml代码如下:

- now = new Date()
- if @view_type == 'my_profile'
  %button.certification-card.add-certification-card{"data-target" => 
"#certificationModal", "data-toggle" => "modal"}
    %i.avatar--icon.fa.fa-plus-circle
    %elabel= ECL.t('add', default_value: "Add")
- _.each @card_data.models, (data) =>
  - expire_date = "#{data.get('valid_till')}"
  - actual_date = new Date(expire_date)
  .certification-card
    .avatar--icon.fa.fa-users
    .details
      .title= "#{data.get('name')}"
      .caption= "#{data.get('place')}"
      - if actual_date < now
        .meta-text= "Valid Till: #{data.get('valid_till')}"
      - else
        .meta-text= "Expired On: #{data.get('valid_till')}"
%button.btn-more.listCertificatesContainer.hide
  Load More
#modalContainer

在这里,我尝试默认显示整个两个认证卡,并仅在单击加载更多时一次加载其余三个。不胜感激

【问题讨论】:

  • 旁注:您可以只做this.card_data = new Backbone.Collection([/*array of objects*/]);,对象将被初始化为模型,而不是手动执行 i 一个一个...
  • 您也可以使用this.$el 代替$(this.el) 和this.$('#modalContainer') 代替$(this.el).find('#modalContainer')

标签: javascript jquery ruby backbone.js haml


【解决方案1】:

你可以这样做(伪代码):

CertificationCard.prototype.initialize = function() {
  this.currentIndex = 0;
  this.loadMoreIncrement = 3
  this.render(2);
};

CertificationCard.prototype.render = function(increment) {
  const newIndex = this.currentIndex + increment;
  cost models = this.card_data.slice(this.currentIndex, newIndex);
  this.$el.append(this.renderCards(models));
  this.currentIndex = newIndex;
};

CertificationCard.prototype.renderCards = function(models) {
  return this.template({
    view_type: this.view_type,
    card_data: models
  });
};

CertificationCard.prototype.renderLoadMore = function() {
  this.render(this.loadMoreIncrement);
};

【讨论】:

  • 这项工作,但问题在于证书卡的附加,它还附加了html元素的其余部分。现在如果我在html中添加一个id并告诉卡只附加在这个 id 上,我可以调用一个方法在该 div 上重复附加。但是 UI 中断了,因为现在卡片只附加在同一个 div 上
  • @MaheshMesta 对不起,我没有完全理解上面的评论,听起来你需要为特定元素渲染一张卡片?目前你所拥有的是我们称之为“CollectionView”的东西,看起来你也需要一个“itemView”。你所做的是分离 HTML 以将 1 个项目呈现到一个新模板,为它创建一个视图(@ 987654322@),然后创建另一个视图来呈现它们的集合 (CertificationCards)。也许我误解了这个问题,但请查看主干中的“渲染集合视图”,如果不是,可能会问一个显示问题的新问题?
  • 是的,我意识到了这一点。为每个项目卡制作了不同的 html 模板。适合我
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 2017-11-18
  • 2022-11-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多