如何处理在骨干模型定制响应

编程入门 行业动态 更新时间:2024-10-15 22:28:23
本文介绍了如何处理在骨干模型定制响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始在我的项目整合骨干。那我有第一个困难是从后端的反应是不是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消费数据。在你的情况,当你有一个 /人端点不返回数组,可以覆盖 Col​​lection.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 如果您需要。

更多推荐

如何处理在骨干模型定制响应

本文发布于:2023-11-25 23:31:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何处理   骨干   模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!