如何确保不会使用backbone.js中的历史记录两次创建模型?

发布于 2024-12-27 15:26:46 字数 892 浏览 4 评论 0原文

在我的路由器中,我有

routes: {
    "create/:type": "create",
    // #create/modeltype
    "diagram/:id": "diagram",
    // #diagram/193
    "": "list"
    //
    },

创建路由

    create: function(type) {
        console.log("diagram create " + type.toUpperCase());

        var newDiagram = new Diagram({
            type: type.toUpperCase()
            });

        newDiagram.save({}, {
            success: function(model, response) {
                console.log("save diagram success");
                window.vnb.routers.workspace.navigate("diagram/" + model.get("id"), true);

            },
            error: function(model, response) {
                console.log("error");
                console.log(model);
                console.log(response);
            }
        });

    },

如果有人创建一个模型然后单击返回,他们会创建另一个模型,我希望这种情况不会发生。

In my router I have

routes: {
    "create/:type": "create",
    // #create/modeltype
    "diagram/:id": "diagram",
    // #diagram/193
    "": "list"
    //
    },

then i have the create route

    create: function(type) {
        console.log("diagram create " + type.toUpperCase());

        var newDiagram = new Diagram({
            type: type.toUpperCase()
            });

        newDiagram.save({}, {
            success: function(model, response) {
                console.log("save diagram success");
                window.vnb.routers.workspace.navigate("diagram/" + model.get("id"), true);

            },
            error: function(model, response) {
                console.log("error");
                console.log(model);
                console.log(response);
            }
        });

    },

If someone creates a model and then clicks back, they create another model, i would like this to not happen.

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

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

发布评论

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

评论(1

堇色安年 2025-01-03 15:26:46

您可以将 newDiagram 变量设置为路由器上名为 diagram 的属性。然后就可以在第一次调用create方法的时候实例化它,之后对create方法的调用就可以使用该变量了。

这是一个例子

create: function(type) {
    console.log("diagram create " + type.toUpperCase());

    if (this.diagram === undefined) {
        this.diagram = new Diagram({
          type: type.toUpperCase()
        });
    }

    this.diagram.save({}, {
        success: function(model, response) {
            console.log("save diagram success");
            window.vnb.routers.workspace.navigate("diagram/" + model.get("id"), true);

        },
        error: function(model, response) {
            console.log("error");
            console.log(model);
            console.log(response);
        }
    });

},

You can make the newDiagram variable into a property on the router called diagram. Then you can instantiate it the first time the create method is called, and the following calls to the create method can use the variable.

Here's an example

create: function(type) {
    console.log("diagram create " + type.toUpperCase());

    if (this.diagram === undefined) {
        this.diagram = new Diagram({
          type: type.toUpperCase()
        });
    }

    this.diagram.save({}, {
        success: function(model, response) {
            console.log("save diagram success");
            window.vnb.routers.workspace.navigate("diagram/" + model.get("id"), true);

        },
        error: function(model, response) {
            console.log("error");
            console.log(model);
            console.log(response);
        }
    });

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