Pergunta

I'm getting a little frustrated with requirejs at this point.

I'm trying to load jquery and other libraries in my App.coffee file. The problem is that I can't load module dependencies in my define. Some of the input arguments (jq, jsn, etc) are null or just HTMLDocument objects. This changes based on the permutation of plugins that I try: 'order', 'domReady', or none.

My js lib directory looks like A). And my App.coffee file looks like B). I've tried to use 'paths' in the require.config and just raw file referencing (what you see below). I'm aware of requirejs-jquery integration. But shouldn't I be able to order my plugin loading?

I get a different set of errors if I try named 'paths'. I see someone suggested jquery 1.7 (also here). Is this working? I first wanted to take a step back and make sure that I have the concepts down correctly. i) require.config ii) module definition w/ dependencies, iii) the order plugin, etc.

If I have these concepts down correctly, I would think it's a jquery version issue. However, it's not just jquery as a dependency. And it's incredibly irksome to lose time in this manner. Any help is appreciated. Thanks in advance.

A)

    $ tree js/lib/
    js/lib/
    ├── backbone.js
    ├── backbone_loader.js
    ├── domReady.js
    ├── jquery-1.6.3.js
    ├── json2.js
    ├── order.js
    ├── pure.js
    ├── require.js
    └── underscore.js

B)

    require.config({
      baseUrl: "/js",
      paths:
        order : '/js/lib/order'
        jQuery : '/js/lib/jquery-1.6.3'
        json2 : '/js/lib/json2'
        Underscore : '/js/lib/underscore'
        Backbone : '/js/lib/backbone_loader'
        pure : '/js/lib/pure'
    })


    define( [ 'js/lib/order!js/lib/jquery-1.6.3',
              'js/lib/order!js/lib/json2',
              'js/lib/order!js/lib/underscore',
              'js/lib/order!js/lib/backbone',
              'js/lib/order!js/lib/pure',
              'js/lib/order!js/bkeeping/models', ]
      (jq, jsn, und, bbn, pur, models) ->

        console.log('bkeeping LOADED')

        # return an object with the models in it  
        models : models
        jQuery : jq.noConflict()
        json2 : jsn 
        Underscore : und.noConflict()
        Backbone : bbn.noConflict()
        pure : pure
    )

Foi útil?

Solução 2

Thanks for the direction on this.

But I went to jquery-1.7, but still had to pull from the global space to get the object. That applies for my other libs for now, and their dependencies. What I ended up doing was returning an object with the dependencies in it. I got library loading done with the order plugin.

The contract of dependency objects being passed into your callback function (jq,pur,jsn, etc), simply isn't working for these libraries. It works for my defined modules (ex: models), but not external libs. I tried the use plugin, but found that it didn't work for underscore and backbone, or I didn't set it up properly. This is my workable solution for now; although I have to say that the headaches are seriously clouding the benefits for require.js.

    define( [ 'order!js/lib/jquery-1.7',
              'order!js/lib/pure',
              'order!js/lib/json2',
              'order!js/lib/underscore',
              'order!js/lib/backbone',
              'order!bkeeping/models', ]
      (jq, pur, jsn, und, bbn, models) ->

        console.log('bkeeping LOADED')

        # return an object with the dependencies in it 
        models : models
        jQuery : jQuery.noConflict()
        pure : pur                      # pure and json2 objects are simply used in other libs. I don't need them directly
        json2 : jsn 
        Underscore : _.noConflict()
        Backbone : Backbone.noConflict()
    )

Outras dicas

The latest jQuery supports AMD loaders and will behave fine with require.js. Underscore and Backbone don't and you will need to either:

  1. use a modified version, for instance the one from the author of require.
  2. use a plugin. I have found the use! plugin from here to fit my needs perfectly.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top