Backbone Marionette:使用emptyView进行加载

azz*_*z0r 5 javascript backbone.js marionette

我正在使用Backbone Marionettes CollectionView.我试图表明集合正在加载,todo所以我使用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"
  })
})
Run Code Online (Sandbox Code Playgroud)

gbs*_*ice 4

我通常做的是监听集合/模型的“请求”和“同步”事件。像这样的东西:

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');
  }
});
Run Code Online (Sandbox Code Playgroud)