使用 ASP .net Web 服务的 Backbone.js,无需 REST
(抱歉英语)
我有一个 ASP .net Web 服务,它从 Oracle 数据库获取数据并返回 JSON 数据。
TestWebService.asmx/getUserData
我使用 jQuery 简单地使用 ajax 请求对此进行了测试
$.ajax({
type:"POST",
data:"{}",
dataType:"json",
contentType:"application/json; charset=utf-8",
url:"TestWebService.asmx/getUserData",
success:function(data){
console.log(data.d);
}
});
。
但现在我想尝试使用 Backbone.js
该应用程序有以下内容:用户数据、文章和购买订单,其中购买订单是文章的集合,所以我认为在 Backbone 的这个模型中,
User = Backbone.Model.extend({})
Article = Backbone.Model.extend({})
ArticleCollection = Backbone.Collection.extend({})
BuyOrder = Backbone.Model.extend({})
BuyOrderCollection = Backbone.Collection.extend({})
视图只是 2。一个表单,其中我显示用户数据和输入以添加文章并创建购买订单和可视化视图以显示购买订单,用户可以在其中单击代码查看并检查一个购买订单的内容。
用户数据和部分文章数据是从服务中获取的:(用户数据如名称和文章数据如代码、描述、价格等)。
¿如何使用这些数据填充 Backbone 模型?
提前致谢。
(sorry for the english)
I have a ASP .net webservice that get data from a oracle database returning JSON data.
TestWebService.asmx/getUserData
I test this using simply ajax request with jQuery
$.ajax({
type:"POST",
data:"{}",
dataType:"json",
contentType:"application/json; charset=utf-8",
url:"TestWebService.asmx/getUserData",
success:function(data){
console.log(data.d);
}
});
This work.
But now i want to try use Backbone.js
The Application have this: User data, Articles and Buy Order where a Buy order is a collection of Articles, so i think in this models for Backbone
User = Backbone.Model.extend({})
Article = Backbone.Model.extend({})
ArticleCollection = Backbone.Collection.extend({})
BuyOrder = Backbone.Model.extend({})
BuyOrderCollection = Backbone.Collection.extend({})
The Views are just 2. A Form where i show the User Data and inputs to add Articles and create the Buy Order and a Visualize view to show the Buy Orders where the user can see an check the content of one buy order clicking in the code.
The UserData, and part of the Article Data are get from the service: (User Data like name and Article Data like code, description, price, etc).
¿How can i populate the Backbone models with this data?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,基本上,您想要覆盖 Backbone.sync。它当前也通过
$.ajax
函数执行 RESTful 操作(GET/POST/PUT/DELETE)。查看默认情况下如何实现:http://documentcloud.github。 com/backbone/docs/backbone.html#section-134如您所知,这确实非常简单......大约 30 行左右的代码将创建/更新/删除/读取映射到在
$.ajax
中发布/放置/删除/获取。现在您已经了解了他们是如何做到的,您只需使用相同的模式来实现您自己的:
一旦您这样做了,您就是黄金。您的模型将执行您希望它们执行的所有 CRUD,并通过 Backbone.sync 的实现进行抽象。
So, basically, you want to override
Backbone.sync
. It is the thing that is currently doing your RESTful stuff (GET/POST/PUT/DELETE) via the$.ajax
function as well. See how it is implemented by default: http://documentcloud.github.com/backbone/docs/backbone.html#section-134As you can tell, it is really quite simple... about 30 or so lines of code to map create/update/delete/read to post/put/delete/get in
$.ajax
.Now that you have seen how they do it, you just implement your own using the same pattern:
Once you do that, you are golden. Your models will do all the CRUD that you want them to, abstracted through your implementation of
Backbone.sync
.