【问题标题】:How to filter a backbone collection and render the results如何过滤主干集合并呈现结果
【发布时间】:2016-01-27 01:49:12
【问题描述】:

Backbone 的新手,如果这是一个简单的问题,请道歉。

我已经成功加载了一个集合并渲染了视图。但是,我有一个带有 A 标签的下拉列表,我想使用过滤器来显示数据。我试图在我的 VIEW 中设置一个事件侦听器,然后在视图中触发一个函数来过滤结果并重新渲染视图。

这是我的代码:

IBA.NewsModel = Backbone.Model.extend({});
IBA.NewsCollection = Backbone.Collection.extend({
    model: IBA.NewsModel,
    url: "/news/api"
});
IBA.NewsView = Backbone.View.extend({
  el: '#main',
  template: _.template($("#news-article").html()),

  events: {
    "change .dropdown-item": "filterNews"
  },

  initialize: function () {
    this.collection = new IBA.NewsCollection();
    this.listenTo(this.collection, 'reset', this.render);
    this.collection.fetch({
      success: function() {
        console.log("JSON file load was successful");
        view.render();
      },
      error: function(){
        console.log('There was some error in loading and processing the JSON file');
      }
    });
  },

  render: function () {
      this.$el.html(this.template({ 
        articles: this.collection.toJSON() 
      })
    );
    return this;
  },

  filterNews: function (e){
    e.preventDefault();
    var items = this.collection.where({cat_name: "interviews"});
    console.log(items);
    this.$el.html(this.template({articles: this.items.toJSON()}));
  }
});

var view = new IBA.NewsView();

【问题讨论】:

    标签: backbone.js


    【解决方案1】:

    最简单的方法是使用过滤结果重置实际集合:

    IBA.NewsModel = Backbone.Model.extend({});
    IBA.NewsCollection = Backbone.Collection.extend({
      model: IBA.NewsModel,
      url: "/news/api"
    });
    IBA.NewsView = Backbone.View.extend({
      el: '#main',
      template: _.template($("#news-article").html()),
    
      events: {
        "change .dropdown-item": "filterNews"
      },
    
      initialize: function() {
        this.collection = new IBA.NewsCollection();
        this.listenTo(this.collection, 'reset', this.render);
        this.fetchNews();
      },
      fetchNews: function() {
        var view = this;
        this.collection.fetch({
          success: function() {
            console.log("JSON file load was successful");
            view.render();
          },
          error: function() {
            console.log('There was some error in loading and processing the JSON file');
          }
        });
      },
      render: function() {
        this.$el.html(this.template({
          articles: this.collection.toJSON()
        }));
        return this;
      },
    
      filterNews: function(e) {
        e.preventDefault();
        this.collection.reset(this.collection.where({
          cat_name: "interviews"
        }));
      }
    });
    
    var view = new IBA.NewsView();
    

    当您想返回原始数据时,请使用fetchNews() 方法再次获取它。

    您还有一个语法错误,即 view 未在您的 initialize 中定义

    【讨论】:

    • 谢谢 TJ。该解决方案效果很好。但是,如果我不想每次都重新加载新闻,你会建议什么方法?我有几个过滤器类别,我假设使用这种方法我每次都必须重新加载数据。我可以对集合进行预排序并将其分配给每个类别的对象然后渲染吗?
    • 由于某种原因,.dropdown-item 上的“点击”事件似乎没有附加。这是一个 Twitter Bootstrap 组件,我猜 TB JS 已经超过了点击事件。你知道我该如何解决这个问题吗?
    • 我刚刚处理了未触发的“click”事件,似乎除非 DOM 通过 Backbone _template 函数呈现,否则它不知道该元素,因此没有注册该事件.这是标准还是某种问题?有解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多