通过触发器实例化 Backbone? (或者如何推迟匿名函数的执行?)
继续寻求了解有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将应用程序的组件放入具有
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:The above assumes you have a global variable called
modalDialog
representing your modal window, which calls itsonOpen
callback when the window is shown.