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. Each Publication has many articles and each article belongs to one publication. Also, each article has a publication_id column.
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. 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?
As Kyle mentioned, you need to make sure that your Rails "API" layer supports having a publication_id as a parameter. You can do this either by modifying your config/routes
file or by checking for the presence of publication_id
in the Rails action.
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.