通过触发器实例化 Backbone? (或者如何推迟匿名函数的执行?)

发布于 2024-12-09 12:56:32 字数 713 浏览 1 评论 0原文

继续寻求了解有关 Backbone.js 的更多信息,我遇到了这样的情况:我希望以编程方式实例化 Backbone 应用程序。出现这种情况的情况是整个网站/页面不是 Backbone 应用程序,而是打开一个模式,然后填充(Backbone 应用程序的基本结构),然后我想触发 Backbone应用程序(当模式打开时,其 JavaScript 也会被加载)。如果可能的话,我更愿意“手动”实例化 Backbone 应用程序,即加载脚本时它不会自动执行。

我一直在使用的典型 Backbone 格式是这样的:

var myApp = (function($) {

    var myModel = Backbone.Model.extend({
        ...
    });

    var modelInstance = new myModel({
        ...
    });        

    var myView = Backbone.View.extend({
        ...
    });

    var viewInstance = new myView({
        ...
    });

}

但是上面的内容在加载脚本文件时会自动执行,然后实例化的视图尝试绑定/渲染到其关联的 DOM 元素等。有没有办法推迟匿名函数的执行,以便我可以在准备模态时加载脚本,然后在[稍微]稍后的时间点触发 Backbone 应用程序?

非常感谢您对此的任何见解。

Continuing my quest to learn more about Backbone.js I have arrived to a situation where I would want to instantiate a Backbone application programmatically. This occurs in a situation where the entire site/page is not a Backbone application, but instead a modal is opened, then populated (base structure for Backbone app), and then I would like to trigger the Backbone application (whose JavaScript was also loaded when the modal was opened). I would prefer to instantiate the Backbone app 'manually' if possible, i.e. so that it would not auto-execute when its script is loaded.

The typical Backbone format I have been using is like this:

var myApp = (function($) {

    var myModel = Backbone.Model.extend({
        ...
    });

    var modelInstance = new myModel({
        ...
    });        

    var myView = Backbone.View.extend({
        ...
    });

    var viewInstance = new myView({
        ...
    });

}

But the above is automatically executed when the script file is loaded, then the instantiated view(s) attempt to bind/render to their associated DOM elements, etc. Is there a way to defer the execution of the anonymous function so that I could load the script when the modal is being prepared but then trigger the Backbone app on command at a [slightly] later point?

Many thanks for any insights on this.

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

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

发布评论

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

评论(1

梦旅人picnic 2024-12-16 12:56:32

您应该将应用程序的组件放入具有 init 方法的对象中,然后可以在模式对话框打开时调用该方法。下面是一个示例结构:

window.App = {
  Models: {},
  Views: {},
  Routers: {},
  init: function () {
    var app = new window.App.Routers.main;
    Backbone.History.start();
    return app;
  }
};

window.App.Models.Foo = Backbone.Model.extend({
  // ...
});

window.App.Views.FooView = Backbone.View.extend({
  // ...
});

window.App.Routers.main = Backbone.Router.extend({
  // ...
});

window.modalDialog.onOpen = function () {
  window.app = window.App.init();
};

上面假设您有一个名为 modalDialog 的全局变量,代表您的模式窗口,该变量在显示窗口时调用其 onOpen 回调。

You should put your app's components into an object with an init method, which you can then call when your modal dialog opens. Here's an example structure:

window.App = {
  Models: {},
  Views: {},
  Routers: {},
  init: function () {
    var app = new window.App.Routers.main;
    Backbone.History.start();
    return app;
  }
};

window.App.Models.Foo = Backbone.Model.extend({
  // ...
});

window.App.Views.FooView = Backbone.View.extend({
  // ...
});

window.App.Routers.main = Backbone.Router.extend({
  // ...
});

window.modalDialog.onOpen = function () {
  window.app = window.App.init();
};

The above assumes you have a global variable called modalDialogrepresenting your modal window, which calls its onOpen callback when the window is shown.

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