Ali*_*Ali 0 php collections json backbone.js
有没有办法通过给fetch()提供初始模型参数来获取集合.
要清除:我有一个模型Human,它具有属性名称(作为字符串)和数字(数组).我想找到我的数据库中所有给定数字的人.(示例:我的数组中有[123,342,4],每个数字我想拉出人名).
我创建了一个Human系列,模型是人类.当我这样取这不会引起任何问题;
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");
}
});
Run Code Online (Sandbox Code Playgroud)
我正在考虑创建一个没有名称和数字的虚拟人类对象,然后将数字传递给我的php,但是当我将一个参数放到开头时,.fetch()函数似乎不起作用.甚至下面的代码都不起作用;
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");
}
});
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?对于我来说,创建一个虚拟人体模型以检索具有给定数字的人类集合是合乎逻辑的.这是我能想到转移特定的所需json数据的唯一方法.
我觉得你搞砸了.
据我所知,数字是Human.ids你想要的.
如果这是正确的,这些数字对于模型没有任何意义Human.我宁愿把它们移到Collection.
你必须准备Collection以在URL中发送一个过滤器参数,该过滤器参数通知服务器层人类想要获取的人类.fetch
此外,您必须准备服务器层,以便能够使用您希望服务器响应的人类的ID来处理过滤器参数.
所以,在Backbone Collection中我们可以使用这样data的fetch()方法选项:
humanCollection.fetch({ data: { ids: [123, 342, 4] } });
Run Code Online (Sandbox Code Playgroud)
该服务器将解析idsPARAM,只返回被请求人.
然后在你的收藏中你将只有你选择的人类,你可以问他们的名字或其他什么.
| 归档时间: |
|
| 查看次数: |
3657 次 |
| 最近记录: |