0

这是我的看法:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/tableModel',
    'collections/tablesCollection',
    'views/tablesView'
], function($, _, Backbone, tableModel, tablesCollection, tablesView) {
    require(['collections/tablesCollection'], function(tablesCollection) {
        var t = new tablesCollection(null, {url: 'main-contact'});
        var tables = new tablesView({ collection: t, template: 'main-contact-template'});
        tables.url = 'main-contact';
        return tables;  // <!-- returns view, tables can be console.log'ed
    });

    console.log(tables); // <!-- tables is not defined
});

我如何tablesrequire声明中获取,以便在哪里可以访问console.log?这似乎是我需要返回视图的地方,所以我可以在我的路线中使用它:

app_router.on('route:index', function(){
        require(['views/tables/mainContactView'], function(mainContactView) {
            console.log(mainContactView);  // <!-- undefined, unless some value is returned from where the console.log() is above
            $('#web-leads').html(mainContactView.render().el);
        });
    });

如果我把它放在return 'test'哪里console.log(tables),我可以console.log()在我的路线上。所以我认为那是我需要返回视图的地方。我如何传递这些数据?现在,即使有一个 return 声明,我的观点也是不确定的。我需要从require. 我怎么做?

4

1 回答 1

0

您已经加载collection/tablesCollection了定义,因此您无需再执行其他require操作来加载它。

您可以简单地将 View 定义为:

define([
    'jquery',
    'underscore',
    'backbone',
    'models/tableModel',
    'collections/tablesCollection',   // You've already required tablesCollection here.
    'views/tablesView'
], function($, _, Backbone, tableModel, tablesCollection, tablesView) {
    var t = new tablesCollection(null, {url: 'main-contact'});
    var tables = new tablesView({ collection: t, template: 'main-contact-template'});
    tables.url = 'main-contact';
    return tables;
});
于 2013-01-30T20:02:41.253 回答