Ali
Ali

Reputation: 5476

Backbone.js Collection fetch with initial parameter

Is there any way to fetch a collection by giving an initial model parameter to fetch().

To clear out: I have a model Human with attributes name (as a string) and numbers(array). I would like to find all the people in my database with given array of numbers. (Example: I have [123,342,4] in my array and for each number I would like to pull the people's name).

I've created a Human collection giving the model is human. And when I fetch like this it causes no problem;

humanCollection.fetch({
            success:function(model,response){
                console.log(model.toJSON().length);
                var arr=model.toJSON();
                for(var i=0;i<arr.length;i++)
                    console.log(arr[i].humanName+" ");
                console.log("Success");
            },
            error:function(model,response){
                console.log(response);
                console.log("Failure");
            }
        });

I'm thinking of creating a dummy human object with no name and just numbers and later on passing the numbers to my php but .fetch() function doesn't seem to work when I put a parameter to the beginning. Not even the code below works;

 humanCollection.fetch({},{
                success:function(model,response){
                    console.log(model.toJSON().length);
                    var arr=model.toJSON();
                    for(var i=0;i<arr.length;i++)
                        console.log(arr[i].humanName+" ");
                    console.log("Success");
                },
                error:function(model,response){
                    console.log(response);
                    console.log("Failure");
                }
            });

What could be the problem? And is it logical for me to create a dummy human model in order to retrieve a collection of humans with the given numbers. That was the only way I could think of transferring the specific required json data.

Upvotes: 0

Views: 3665

Answers (1)

fguillen
fguillen

Reputation: 38792

I think you are messing up things.

As I understood the numbers are the Human.ids you want to fetch.

If this is correct these numbers have not any meaning to be part the the Human model. I rather will move them to the Collection.

You have to prepare the Collection to send a filter param in the URL of the fetch that informs to the server layer which Humans the Collection wants to fetch.

Also you have to prepare the server layer to be able to process the filter param with the ids of the Humans you want the server responses with.

So, in the Backbone Collection we can play with the data option of the fetch() method like this:

humanCollection.fetch({ data: { ids: [123, 342, 4] } });

The server will have to parse the ids param and return only the requested Humans.

Then in your Collection you will only have your selected Humans and you will can ask them for name or whatever.

Upvotes: 5

Related Questions