dongzun9958 2012-05-20 22:33
浏览 45
已采纳

Backbone.js使用初始参数获取集合

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.

  • 写回答

1条回答 默认 最新

  • dongza3124 2012-05-21 10:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32c8t6工程,使用hal库
  • ¥100 有偿求易语言word文档取doc和docx页数方法或模块
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决