我开始在我的项目整合骨干。那我有第一个困难是从后端的反应是不是JSON数组或不适合骨干。下面是一个例子。
I started integrating backbone in my project. The very first difficulty that i had was response from backend was not JSON Array or not designed for backbone. Here is an example.
//A backbone model var Person = Backbone.Model.extend({}); // A backbone collection var PersonCollection = Backbone.Collection.extend({ model : Person, url: '/people' });所以考虑这个,那个时候我的请求/人不返回的人JSON阵列。相反,它返回类似:
So consider this, that when I request /people it does not return JSON array of people. Instead it return something like:
{header: "some str", people: ["person", "array", ".."], stats: "something is here" }它的问题是骨干无法分配此JSON响应模型。有没有可以在控制器上做任何回应TWEAK。因此,访问模式可以是正常的。任何前/后挂机。
The problem with it is backbone is unable to assign this JSON response to models. Is there any tweak that can be done in controller on response. So accessing model can be normal. Any before/after hook.
FYI:骨干正从服务器的响应,我可以看到它在responseText的键。
FYI: backbone is getting response from server, I can see it under "responseText" key.
任何帮助是非常AP preciated。
Any help is highly appreciated.
推荐答案骨干支持这一点。我遇到同样的问题,当从Parse消费数据。在你的情况,当你有一个 /人端点不返回数组,可以覆盖 Collection.parse 函数来显示骨干如何找到它正在寻找数组:
Backbone supports this. I ran into the same issue when consuming data from Parse. In your case, when you have a /people endpoint that does not return an array, you can override the Collection.parse function to show Backbone how to find the array it is looking for:
var PersonCollection = Backbone.Collection.extend({ model : Person, url: '/people', parse: function(resp, xhr) { this.header = resp.header; this.stats = resp.stats; return resp.people; } });那么你应该如果你需要做更多的在这个函数。以类似的方式,您可以覆盖 Model.sync 如果您需要。
更多推荐
如何处理在骨干模型定制响应
发布评论