简体   繁体   English

如何确保在深度模块化的Backbone,Require.js和D3应用程序中先调用某个函数?

[英]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. 我正在开发一个模块化的javascript应用程序,除了一个细节外,其他所有内容看起来都非常整洁:仅在使用Backbone.collection.fetch()方法从服务器中获取了模型后,我才能弄清楚如何加载initialize函数。

Please have a look into this file and possibly the whole project if you will, I appreciate a lot your collaboration. 如果您愿意的话,请查看文件,甚至可能查看整个项目 ,非常感谢您的合作。

fetch has a success callback, as Reed Spool pointed out. 正如Reed Spool指出的那样, fetch具有成功的回调。 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
    }      

});

If I'm hearin you right, I think you need to use a "Success" callback function in fetch. 如果我没听错,我认为您需要在提取中使用“成功”回调函数。 See here #2 http://chilipepperdesign.com/2013/01/15/backbone-js-bind-callback-to-successful-model-fetch/ 参见此处#2 http://chilipepperdesign.com/2013/01/15/backbone-js-bind-callback-to-successful-model-fetch/

If it's decoupling function timing in modularized code you're worried about, as your question's header suggests, I suggest using Backbone's Events. 如果您担心的是模块化代码中的函数计时解耦,正如您的问题的标题所建议的那样,我建议您使用Backbone的事件。 See the Event Aggregator, or Pub-Sub design patterns. 请参阅事件聚合器或Pub-Sub设计模式。

The conventional way to get around this using Backbone, is to use events. 使用Backbone解决此问题的常规方法是使用事件。 I've based this off the various blog posts of MarionetteJS founder Derek Bailey, so I'll add references when I find the relevant ones later... 我是基于MarionetteJS创始人Derek Bailey的各种博客文章撰写的,因此以后找到相关参考文献时,我将添加参考文献...

Your view would do this: 您的视图将执行以下操作:

var view = Backbone.View.extend({
    ...
    _actuallyInitialize: function () {
        ...
    }
    initialize: function (attr, options) {
        ...
        this.listenTo(this.model, 'data:fetched', this._actuallyInitialize);
        ...
    }
});

And the code that is fetching your model, would do this: 而获取模型的代码将执行以下操作:

var jqXHR = model.fetch();
jqXHR.done((function (bindModel) {
    return function (response, textStatus, jqXHR) {
        ...
        bindModel.trigger('data:fetched');
    };
}(model)));

This way, you maintain the high-level modular approach, decouple the code (any number of views can update themselves on fetch now), and maintain readability. 这样,您可以维护高级模块化方法,解耦代码(任何数量的视图都可以在立即fetch进行更新),并保持可读性。 Your Model does stuff to itself, and your View does stuff to itself. 您的Model自行完成, View则自行完成。 Pretty neat, in my opinion, and one of the patterns I love using in Backbone. 在我看来,这非常整洁,是我喜欢在Backbone中使用的模式之一。 Thanks for the Q. 谢谢你的问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Backbone.js和require.js:如何将我的模型,视图和集合转换为require.js模块? - Backbone.js & require.js: how do I transform my models, views and collections to require.js modules? 如何确保在调用js函数之前将动态生成的div加载到DOM中? - How do I make sure dynamically generated div is loaded into DOM before js function is called on it? 使用Backbone.js和require.js的jQuery插件 - 如何将“this”变量传递给依赖项? - jQuery plugin using Backbone.js and require.js - How do I pass the “this” variable to a dependency? 带有require.JS的主干如何工作? - How does backbone with require.JS work? 如何将Backbone.js与Require.js(r.js)一起使用,但在优化后会产生2个文件? - How do I use Backbone.js with Require.js (r.js), but resulting in 2 files after I optimize it? Require.js + Backbone优化 - Require.js + Backbone optimization 在调用另一个函数之前,请确保已加载JS文件并调用了JS函数 - Make sure JS file is loaded and JS function is called BEFORE calling another function 如何在require.js中定义一个函数并加载它? - How do you define a function in require.js and load it? require.js + backbone.js:如何构造具有初始化函数的模块? - require.js + backbone.js: How to structure modules that have an initialize function? 使用Require.js和Backbone定义全局App Namespace - Defining a global App Namespace with Require.js and Backbone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM