Intermittent RequireJS Load Error

前端 未结 4 2051
春和景丽
春和景丽 2021-02-02 13:54

I have a rather large Backbone.js project that uses RequireJS. As the project size grew (\"size\" here referring to the number of separate module files), intermittent errors sta

4条回答
  •  孤街浪徒
    2021-02-02 14:29

    I've been fighting with the same issue the last days and this is what I found out:

    Apparently, a nested dependency structure requiring templates via the text.js plugin can cause a race condition that lets the top level module not be ready when requirejs thinks it is. I only ran into this problem when I had several nested module dependency structures of this type:

    Router
      -> View1
        -> text!.../view1.html
        -> View2
          -> text!.../view2.html
      -> View3
        -> text!.../view3.html
        -> View4
          -> text!.../view4.html
      -> View5
        -> text!.../view5.html
        -> View6
          -> text!.../view6.html
      -> View7
        -> text!.../view7.html
        -> View8
          -> text!.../view8.html
    

    Having this structure, I got TypeErrors like 'View1 is not a constructor' when the router tries to instantiate the views.

    Also requiring the templates of the nested views in the top level views solved the problem for me:

    Router
      -> View1
        -> text!.../view1.html
        -> text!.../view2.html
        -> View2
          -> text!.../view2.html
      -> View3
        -> text!.../view3.html
        -> text!.../view4.html
        -> View4
          -> text!.../view4.html
      -> View5
        -> text!.../view5.html
        -> text!.../view6.html
        -> View6
          -> text!.../view6.html
      -> View7
        -> text!.../view7.html
        -> text!.../view8.html
        -> View8
          -> text!.../view8.html
    

    I don't really know how require.js works, but this looks to me like those nested text! calls aren't regarded when some 'ready' flag for the parent module is set.

提交回复
热议问题