因此,我可以从数据库中获取一个集合,并使用以下代码显示它:
var articles = new SimpleGoogleReader.Collections.Articles();
articles.fetch({
success: function(articles){
var view = new SimpleGoogleReader.Views.ArticlesIndex({model: articles});
view.render();
}
});而且效果很好。
在我的Rails模型中(切换到rails片刻),我有一个发布模型和一个文章模型。每个出版物都有许多文章,每一篇文章都属于一个出版物。另外,每一篇文章都有一个publication_id列。
回到脊梁。现在,我想要做的是,从主干中获取我的文章集合,但只获取具有指定发布id的文章。以下是我所拥有的:
articles_by_id: function(id){
var articles = new SimpleGoogleReader.Collections.Articles();
articles.fetch({
data: {publication_id: id},
success: function(x){
console.log(x);
}
});
}这仍然是得到我所有的文章,而不是过滤的版本,我正在寻找。目前,我只想将数据打印到控制台,看看是否得到了正确的数据。稍后我将处理呈现视图的问题。
有什么想法吗?
发布于 2014-02-13 21:52:49
正如凯尔所提到的,您需要确保Rails "API“层支持将publication_id作为参数。您可以通过修改config/routes文件或检查Rails操作中是否存在publication_id来做到这一点。
例如,您可能有这样的东西:
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
end只需将其改为:
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这不是惯用的,通常您只需应用where的条件类似于这里的答案,How to add conditional where clauses in rails,但它适用于这种情况。
发布于 2014-02-13 21:44:09
你的API允许过滤吗?传递数据选项将简单地将它们添加到请求中,例如,您的请求如下所示。
http://www.yourdomain.com/articles?publication_id=id
但是,您可以获取所有这些信息,然后过滤您的集合客户端。
articles_by_id: function(id){
var articles = new SimpleGoogleReader.Collections.Articles();
articles.fetch({
success: function(x){
console.log(x.where({publication_id: id}));
}
});
}https://stackoverflow.com/questions/21763951
复制相似问题