1

我试图允许用户在集合中搜索 displayNames 和电子邮件。

到目前为止,我的整个复合视图看起来像下面的代码。这会渲染并记录我的 var 搜索,但我不确定如何使用 collection.where 以及在新的 Backbone.Collection 之后,我是否调用渲染?

define(["marionette", "text!app/templates/bamboo/employees/collection.html", "app/collections/bamboo/employees",
  "app/views/bamboo/employees/item", "jquery"],
  function(Marionette, Template, Collection, Row, $) {
    "use strict"
    return Backbone.Marionette.CompositeView.extend({
      template: Template,
      itemView: Row,
      itemViewContainer: "ul",
      collectionEvents: {
        'sync': 'hideLoading'
      },
      events: {
        'keyup #filter-input': 'initialize'
      },
      initialize: function () {
        var search = $('#filter-input').val()

        if (typeof(search) != "undefined") {
          console.log(search)
          var filtered = //somehow search collection displayName and email by value search
          this.collection = new Backbone.Collection(filtered);
        } else {
          this.showLoading()
          this.collection = new Collection()
          return this.collection.fetch()
        }
      },
      showLoading: function() {
        this.$el.addClass('loading')
      },
      hideLoading: function() {
        this.$el.removeClass('loading')
      }
  })
})
4

2 回答 2

2

您可以从Marionette CollectionViewCompositeView使用view.children._views.

使用这样的代码:

_.each(colView.children._views,function(v){
  if(!condition){
     v.$el.hide();
  }
});

您可以隐藏colView没有条件的视图(在您的情况下可能是v.model.get('type') == 'todo')。

于 2013-10-22T15:42:42.093 回答
1

我认为如果您只实例化集合的单个实例,然后在模型上调用函数,这可能会更容易。

return Backbone.Marionette.CompositeView.extend({
    template: Template,
    itemView: Row,
    itemViewContainer: "ul",
    events: {
        'keyup #filter-input': 'filter'
    },
    initialize: function() {
       this.filter();
    },
    filter: function () {
        var search = $('#filter-input').val() || '';
        this.collection.invoke('set', 'filter', search);
    },
    // ...
});

然后,对于您的 itemViews

Row = Backbone.Marionette.ItemView.extend({
    modelEvents: {
        "change filter": "filter"
    },
    filter: function(model, search) {
        if (model.shouldBeShown(search)) {
            this.$el.show();
        } else {
            this.$el.hide();
        }
    }
});
于 2013-10-22T20:15:46.340 回答