使用 ASP .net Web 服务的 Backbone.js,无需 REST

发布于 2024-12-13 07:51:06 字数 958 浏览 0 评论 0原文

(抱歉英语)

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2024-12-20 07:51:06

因此,基本上,您想要覆盖 Backbone.sync。它当前也通过 $.ajax 函数执行 RESTful 操作(GET/POST/PUT/DELETE)。查看默认情况下如何实现:http://documentcloud.github。 com/backbone/docs/backbone.html#section-134

如您所知,这确实非常简单......大约 30 行左右的代码将创建/更新/删除/读取映射到在 $.ajax 中发布/放置/删除/获取。

现在您已经了解了他们是如何做到的,您只需使用相同的模式来实现您自己的:

Backbone.sync = function(method, model, options) {
    // your implementation
};

一旦您这样做了,您就是黄金。您的模型将执行您希望它们执行的所有 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-134

As 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:

Backbone.sync = function(method, model, options) {
    // your implementation
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文