Search code examples
javascriptbackbone.jsd3.jsrequirejsamd

How do I make sure a function will be called before another in a deeply modularized Backbone, Require.js and D3 app?


Here is my headache. I'm developing a modularized javascript app and everything seems pretty neat except for one detail: I just can figure out how to load my initialize function only after my models have been fetched from the server with this Backbone.collection.fetch() method.

Please have a look into this file and possibly the whole project if you will, I appreciate a lot your collaboration.


Solution

  • fetch has a success callback, as Reed Spool pointed out. Here's how you could use it:

    var View = Backbone.View.extend({
    
        initialize: function () {
          // Preserve *this* in closures
          var that = this;
    
          this.collection.fetch({
            success: function(data){
              console.log(data);            
              that.render();
          },
            error: function(){
              console.log('error');
            }
          });
    
        },
    
        render: function() {
          //render whatever you need from your fetched collection
        }      
    
    });