Backbone.js + require.js 应用程序:不返回加载了 require.js 的模块视图

发布于 2024-12-29 12:50:39 字数 1406 浏览 0 评论 0原文

我有两个骨干观点。一个是主视图,另一个是将在主视图中使用的视图。我正在为我的不同观点制作单独的模块。我已经从路由器加载并运行良好的主视图。这是我的两个视图:

主视图:

 define([
    'jquery',
    'backbone',
    'metros/docsMetro'
], 
function($, Backbone, docsMetro) {
    //Main Dashboard View
    var Dashboard = Backbone.View.extend({
            el: $('#mainContent'),
            events: {

            },


            initialize: function() {
                console.log('test');
                //Validate User Here most likely.  Each of the main view's for each app should probably call the same validation function


            },

            render: function (){
                console.log('testing render');

            }

        });


    // Return the Main Dashboard View
    return new Dashboard;

正在加载的“metros/docsMetro”文件位于顶部的 require 中。我可以看到这个视图正在加载并且正在通过 init 运行。

define([
    'jquery',
    'backbone'
], 
function($, Backbone) {
    //Docs Metro View
    var docsMetro = Backbone.View.extend({
            el: $('.docs'),
            events: {},

            initialize: function() {
                console.log('docs Metro');

            },

            render: function (){
                console.log('redering docs');

            }

        });

    // Return the View
    return new docsMetro;
});

我的问题是在主视图中,应该是返回的 docsMetro 视图的“docsMetro”变量返回 null。

我缺少什么,似乎所有设置都正确?

I have two backbone Views. One is the main view and the other is a view that will be used within the main view. I am making individual modules for my various views. I have the main view being loaded and run fine from the router. Here are my two views:

MAIN VIEW:

 define([
    'jquery',
    'backbone',
    'metros/docsMetro'
], 
function($, Backbone, docsMetro) {
    //Main Dashboard View
    var Dashboard = Backbone.View.extend({
            el: $('#mainContent'),
            events: {

            },


            initialize: function() {
                console.log('test');
                //Validate User Here most likely.  Each of the main view's for each app should probably call the same validation function


            },

            render: function (){
                console.log('testing render');

            }

        });


    // Return the Main Dashboard View
    return new Dashboard;

The 'metros/docsMetro' file that is being loaded with require there in the top. I can see that this view is loading and is running through the init.

define([
    'jquery',
    'backbone'
], 
function($, Backbone) {
    //Docs Metro View
    var docsMetro = Backbone.View.extend({
            el: $('.docs'),
            events: {},

            initialize: function() {
                console.log('docs Metro');

            },

            render: function (){
                console.log('redering docs');

            }

        });

    // Return the View
    return new docsMetro;
});

My issue is in the Main View the 'docsMetro' variable that should be the returned docsMetro view is coming back null.

What am I missing, seems like it is all setup correctly?

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

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

发布评论

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

评论(1

居里长安 2025-01-05 12:50:39

您可以尝试将模块的路径设置为相对路径吗?

而不是执行 'metros/docsMetro' 执行 './metros/docsMetro' (假设 Metros 文件夹与您需要 Metros/docsMetro 的模块文件位于同一级别) )

以及一些针对您的代码的其他提示和修复。

模块应该返回构造函数而不是实例,并且您应该在需要它们的视图的初始值设定项中实例化它们。

jQuery 和 Backbone 不会放弃对其名称空间的全局注册的支持,因此您不需要每次都将它们传递到依赖项数组。如果您在加载路由器和主视图之前在 boostrap 中需要它们一次就足够了,主视图将负责加载应用程序的其余部分

can you try setting paths to modules to be relative?

instead of doing 'metros/docsMetro' do './metros/docsMetro' (assuming metros folder is on the same level as the module file from which you require metros/docsMetro)

And few other tips and fixes for your code.

Modules should return constructors rather then instances and you should instantiate them in the initializers for views that are requiring them.

jQuery and Backbone aren't going to drop support for global registering of their namespace so you don't need to pass them every time to the dependencies array. It's enough if you require them once in the boostrap before you load your router and main view which will take care of loading the rest of the app

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