我正在使用Backbone Marionettes CollectionView。我试图指示该集合正在加载,所以我正在使用emptyView显示加载器。

但是,这是错误的逻辑,因为有时集合为空,因此它成为损坏的加载器。

我尝试使用此脚本,但没有成功:https://github.com/surevine/marionette-loadingview-plugin

有谁有更好的解决方案?这是我当前的代码:

//loadingview
define(["marionette", "text!app/templates/loading.html"], function(Marionette, Template) {
  "use strict";
  return Backbone.Marionette.ItemView.extend({
    template: Template
  });
})

//collection
define(["marionette", "text!app/templates/events/collection.html", "app/collections/events", "app/views/events/item", 'app/views/loading'], function (Marionette, Template, Collection, Row, LoadingView) {
  "use strict"
  return Backbone.Marionette.CompositeView.extend({
    template: Template,
    itemView: Row,
    itemViewContainer: "ul",
    emptyView: LoadingView,
    initialize: function () {
      this.collection = new Collection()
      return this.collection.fetch()
    }
  })
})

//item
define(["marionette", "text!app/templates/events/item.html"], function(Marionette, Template) {
  "use strict";
  return Backbone.Marionette.ItemView.extend({
    template: Template,
    tagName: "li"
  })
})

最佳答案

我通常要做的是监听Collection/Model的“request”和“sync”事件。像这样的东西:

var View = Backbone.Marionette.CompositeView.extend({
  template: Template,
  itemView: Row,
  itemViewContainer: "ul",

  collectionEvents: {
    'request': 'showLoading',
    'sync': 'hideLoading'
  },

  initialize: function () {
    this.collection = new Collection();
    return this.collection.fetch();
  }

  showLoading: function() {
    this.$el.addClass('loading');
  },

  hideLoading: function() {
    this.$el.removeClass('loading');
  }
});

关于javascript - Backbone Marionette : using emptyView for loading,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19053708/

10-11 23:23