4

所以我的问题是,从我的路由器通风口/事件聚合器触发的函数中的集合无法访问我的主集合的获取模型。

我的猜测是这是一个异步调用问题,但我怎样才能做到这一点,以便通风函数调用 WAITS,直到在执行之前获取集合/模型?或者这甚至是我的问题?

这是我的相关代码。我正在使用 require.js 和主干创建模块化 AMD 应用程序。非常感谢您:

main.js

require(['views/app'], function (AppView) {
window.App = {
    Vent : _.extend({}, Backbone.Events)
};
new AppView();

路由器.js

    define([
        'backbone',
    ], function(Backbone){

    var MainRouter = Backbone.Router.extend({
    routes: {
      'levelone/:id':'showWork'
    },

    showWork: function (index){
      App.Vent.trigger('addressChange', {
        index: index
      });
   }
});
   return MainRouter;
});

应用程序.js

define([
'backbone',
'views/levelone/LevelOneView',
'views/leveltwo/LevelTwoView',
'views/static/StaticView',
'router'
],
function(Backbone, LevelOneView, LevelTwoView, StaticView, MainRouter){
var AppView = Backbone.View.extend({
    el: $("body"),

    events: {
               ...
    },
    initialize: function(){                 
        new LevelOneView();
        App.router = new MainRouter();
        Backbone.history.start();
    },
   .............

LevelOneView.js

initialize:function() {
    this.getCollection();   
    this.domSetup();

    App.Vent.on('addressChange', this.addressChange, this);
},
getCollection : function(){
    var self = this;
    onDataHandler = function(collection) {
        self.LevelTwoCollectionGrab();
    };

    this.collection = new LevelOneCollection([]);
    this.collection.fetch({ success : onDataHandler, dataType: "jsonp" });
},
// We grab a Level Two Collection here so we can take the ids from it and add them to our Level One collection.
// This is necessary so we can create links between the two levels.
LevelTwoCollectionGrab: function(){
    var self = this;

    this.leveltwocollection = new LevelTwoCollectionBase([]);

    onDataHandler = function(collection){
        self.render();
        self.$el.animate({
            'opacity': 1
        }, 1200);
        self.renderLevelTwoIds();
        self.setLevelTwoids();
        self.attachLevelTwoLink();
    }
    this.leveltwocollection.fetch({success : onDataHandler, dataType: "jsonp"});
},
renderLevelTwoIds: function(){
    return this;
},
render: function(){
    var pathname = window.location.hash;

    this.setModelId(this.collection.models);
    this.addPositionsToIndex();
    this.determineModels();
    this.attachLevelTwoLink();
    ....... 
},      

addressChange: function(opts){

console.log(this.collection.models)
//returns a big fat empty array. WHY?!
}
4

2 回答 2

1

您可以使用返回的 jQuery Promisesfetch来帮助您了解何时获取两个集合。

initialize:function() {
    this.getCollection();   
    this.domSetup();

    App.Vent.on('addressChange', this.addressChange, this);
},
getCollection : function(){
    var self = this;

    console.log('should be first');

    this.collection = new LevelOneCollection([]);
    this.fetchingLevelOne = this.collection.fetch({ dataType: "jsonp" });
    this.fetchingLevelTwo = this.leveltwocollection.fetch({ dataType: "jsonp"});

    // wait for both collections to be done fetching.
    // this one will always be called before the one in addressChange
    $.when(this.fetchingCollectionOne, this.fetchingCollectionTwo).done(function(){
        console.log('should be second');
        self.render();
        self.$el.animate({
            'opacity': 1
        }, 1200);
        self.renderLevelTwoIds();
        self.setLevelTwoids();
        self.attachLevelTwoLink();
    });
},

renderLevelTwoIds: function(){
    return this;
},
render: function(){
    var pathname = window.location.hash;

    this.setModelId(this.collection.models);
    this.addPositionsToIndex();
    this.determineModels();
    this.attachLevelTwoLink();
    ....... 
},      

addressChange: function(opts){
    var self = this;

    // wait for both collections to be done fetching.
    // this one will always be called AFTER the one in getCollection
    $.when(this.fetchingCollectionOne, this.fetchingCollectionTwo).done(function(){
        console.log('should be third');
        console.log(self.collection.models);
    });
}

这样做的好处是,如果用户在地址栏中输入的速度非常快,并且进行了多次addressChange调用,它们都会等到集合被提取并以正确的顺序执行。

于 2013-03-21T19:31:12.940 回答
0

我想我解决了。基本上,我现在在 $.when 函数中调用该函数——

像这样:

    $.when(this.collection.fetch(), this.leveltwocollection.fetch()).done(function(){
        $.when(self.render()).done(function(){
            _.each(self.collection.models, function(model){
                var wpid = model.get('id'),
                    bbid = model.id;
                if (wpid == index){
                    window.App.InfoPos.pos5 = bbid;
                    var modelinfo = model.toJSON();
                    $('.box5').empty();
                    $('.box5').html(tmplOne(modelinfo));
                    self.$el.animate({
                        'opacity': 1
                    }, 1200);
                }
            });             
        });
    });

该函数从 when 调用内部启动,然后等待直到完成,然后再执行 done 函数中的任何内容。现在工作!谢谢大家的帮助,尤其是你保罗。

于 2013-03-22T16:00:00.667 回答