I have the following function:
addBook : function() {
_authors = new Array();
authors = $("#id_authors").val().split(",");
for (var i=0;i<authors.length;i++) {
var firstname = authors[i].split(" ")[0];
var lastname = $.trim(authors[i].replace(firstname,""));
_authors.push({"firstname": firstname, "lastname": lastname});
}
this.authors.fetch({data: _authors});
console.log(this.authors);
return false;
}
The value in #id_authors is Leo Tolstoy,Richard Pevear,Larissa Volokhonsky. I would like to loop through these and grab the author information for only these authors.
What is happening is it’s grabbing the first 20 authors (which don’t happen to be these authors). How do I use the collection to grab only the 3 authors that were specified?
I’m not sure which version of Backbone you’re running but it looks like as of 0.9.2 you probably want to use “reset” instead of “fetch” as “reset” takes a list of models to reset.
When you run .fetch(), it should be running your Backbone.sync() function which may not be able to send the right query to request just those 3 authors.
You should check your sync function (if you defined one) and see what the url looks like when you try to fetch those 3 authors.
If you’re using the default RESTful way of loading your data, then make sure it is passing the right resource argument(s) to query just those 3 authors.