简体   繁体   English

过滤Backbone.js集合

[英]Filtering a Backbone.js Collection

So I can fetch a collection from the database and display it using this code: 因此,我可以从数据库中获取一个集合并使用以下代码显示它:

var articles = new SimpleGoogleReader.Collections.Articles();
articles.fetch({
  success: function(articles){
  var view = new SimpleGoogleReader.Views.ArticlesIndex({model: articles});
  view.render();
  }

});

and it works just fine. 而且效果很好。

In my Rails models (switching to rails for a sec) I have a publication model and an article model. 在我的Rails模型中(几秒钟切换到Rails),我有一个发布模型和一个文章模型。 Each Publication has many articles and each article belongs to one publication. 每个出版物都有很多文章,并且每个文章都属于一个出版物。 Also, each article has a publication_id column. 此外,每篇文章都有一个publication_id列。

Back to Backbone. 回到骨干。 Now what I would like to do is, from Backbone, fetch my articles collection but only the articles that have a specified publication id. 现在,我想从Backbone处获取我的文章集,但仅获取具有指定发布ID的文章。 Here is what I have: 这是我所拥有的:

  articles_by_id: function(id){
    var articles = new SimpleGoogleReader.Collections.Articles();
    articles.fetch({
      data: {publication_id: id},
      success: function(x){
        console.log(x);
      }
    });
  }

This is still getting me all of the articles and not the filtered version I am looking for. 这仍然让我获得所有文章,而不是我正在寻找的过滤版本。 For the moment I just want to print the data to the console to see if I am getting the proper data. 目前,我只想将数据打印到控制台以查看是否获得了正确的数据。 I will deal with rendering views later. 稍后我将处理渲染视图。

Any ideas? 有任何想法吗?

Does your API allow for filtering? 您的API是否允许过滤? passing data options will simply add them to the request for example your request would look like this. 传递数据选项只会将其添加到请求中,例如您的请求将如下所示。

http://www.yourdomain.com/articles?publication_id=id

You can however fetch all of them and then filter your collection client side. 但是,您可以获取所有这些内容,然后过滤您的集合客户端。

  articles_by_id: function(id){
    var articles = new SimpleGoogleReader.Collections.Articles();
    articles.fetch({
      success: function(x){
        console.log(x.where({publication_id: id}));
      }
    });
  }

As Kyle mentioned, you need to make sure that your Rails "API" layer supports having a publication_id as a parameter. 如Kyle所述,您需要确保Rails的“ API”层支持将publication_id作为参数。 You can do this either by modifying your config/routes file or by checking for the presence of publication_id in the Rails action. 您可以通过修改config/routes文件或通过检查Rails操作中是否存在publication_id来做到这一点。

For example, you probably have something like this: 例如,您可能会遇到以下情况:

class ArticlesController < ApplicationController
  def index
    @articles = Article.all
  end
end

just change that to: 只需将其更改为:

class ArticlesController < ApplicationController
  def index
    if params[:publication_id]
      @articles = Article.where(:publication_id => params[:publication_id]).all
    else
      @articles = Article.all
    end
  end
end

This isn't idiomatic, usually you would just apply the where 's conditionally similar to ths answer here How to add conditional where clauses in rails but it'll work for this situation. 这不是习惯用法,通常您只需要where条件上应用where类似于在这里的答案如何在rails中添加条件where子句,但在这种情况下它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM