使用backbonejs和jquery将模型同步到asp.net服务器

发布于 2024-12-08 19:30:31 字数 1889 浏览 0 评论 0原文

我想看看当在扩展模型上指定 urlRoot 时如何使用 model.save() 方法将模型保存到服务器,但是当我询问时,ajax 请求永远不会触发对于 model.fetch()do model.save()。注意:我想在不使用 Collection 的情况下希望这是可能的吗?

HTML

<div id="placeholder"></div>
<script type="text/template" id="view_template">
    Hello <%= name %>, here is your script <%= script %>
</script>

模型

 window["model"] = Backbone.Model.extend({
        initialize: function () {
            console.log("CREATED");
        },
        defaults:{
            name:"Please enter your name",
            script:"Hello World"
        },
        urlRoot: "index.aspx",
        validate: function (attrs) {

        },
        sync: function (method, model, success, error) {
            console.log("SYNCING", arguments);
        }
    });

视图

 window["view"] = Backbone.View.extend({
        template:_.template($("#view_template").html()),
        initialize: function () {
            console.log("INITIALISED VIEW");
            this.model.bind("change","render",this);
        },
        render: function (model) {
                console.log("RENDERING");
                $(this.el).append(this.template(model));
                return this;
        }
    });

应用程序

$("document").ready(function () {

    var myModel = new model({
        name: "Stack Overflow",
        script: "alert('Hi SO')"
    });

    var myView = new view({
        model: myModel,
        el: $("#placeholder")

    });

    console.log("SAVING");
    myModel.save();        
    console.log("FETCHING");
    myModel.fetch();


});

,正如您在我调用 save & 的应用程序中看到的那样fetch 但根据文档,这应该使用 POST -> 触发 ajax 请求保存 & 获取 ->获取。但它所做的只是将同步函数中的参数记录到控制台中。

I wanted to see how i could save a model to server using model.save() method when urlRoot is specified on the extended model, but ajax request never fires when i ask for model.fetch() or do model.save(). note: Is hope this is possible without using Collection i suppose?.

HTML

<div id="placeholder"></div>
<script type="text/template" id="view_template">
    Hello <%= name %>, here is your script <%= script %>
</script>

Model

 window["model"] = Backbone.Model.extend({
        initialize: function () {
            console.log("CREATED");
        },
        defaults:{
            name:"Please enter your name",
            script:"Hello World"
        },
        urlRoot: "index.aspx",
        validate: function (attrs) {

        },
        sync: function (method, model, success, error) {
            console.log("SYNCING", arguments);
        }
    });

View

 window["view"] = Backbone.View.extend({
        template:_.template($("#view_template").html()),
        initialize: function () {
            console.log("INITIALISED VIEW");
            this.model.bind("change","render",this);
        },
        render: function (model) {
                console.log("RENDERING");
                $(this.el).append(this.template(model));
                return this;
        }
    });

Application

$("document").ready(function () {

    var myModel = new model({
        name: "Stack Overflow",
        script: "alert('Hi SO')"
    });

    var myView = new view({
        model: myModel,
        el: $("#placeholder")

    });

    console.log("SAVING");
    myModel.save();        
    console.log("FETCHING");
    myModel.fetch();


});

as you can see in application i call save & fetch but as per documentation this should fire ajax request with POST -> SAVE & GET -> FETCH. But all it does is log's arguments into console in the sync function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

站稳脚跟 2024-12-15 19:30:31

我认为您没有看到任何 Ajax 请求的唯一原因是您已经覆盖了 Model.sync 方法。通常,只有当您想要替换 Backbone.sync 中实现的默认 Ajax 同步时,您才会执行此操作。请参阅backbone.js 中的 Model.fetch 中的以下行:

return (this.sync || Backbone.sync).call(this, 'read', this, options);

我对您的代码进行了快速测试,如果我重命名您的 Model.sync 方法,我会看到 Ajax 请求。

I think the only reason you are not seeing any Ajax requests is that you have overridden the Model.sync method. Normally you would only do this if you wanted to replace the default Ajax syncing implemented in Backbone.sync. See the following line in Model.fetch in backbone.js:

return (this.sync || Backbone.sync).call(this, 'read', this, options);

I did a quick test with your code and I am seeing the Ajax requests if I rename your Model.sync method.

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