swap underscore 1.8.3 for lodash 4.2.1 in backbone marionette 2.4.4
Asked Answered
E

1

6

Is this even possible? Keep reading conflicting reports on this.

I have a Marionette app, just upgraded to 2.4.4.

If I drop in lodash in place of underscore - using requireJS,

 map: {
  '*': {
    'underscore': 'lodash'
  }
},

//  'underscore':'/resource/vendor/backbone.marionette/underscore',
'lodash':'/resource/vendor/lodash/lodash.min',

I get the following error...

Uncaught TypeError: Cannot read property 'vent' of undefined

lodash is loading up ok, just marionette is complaining.

It appears that the context this on line 466 is undefined

 463 _proxyMethods: function() {
 464     _.each([ "vent", "commands", "reqres" ], function(system) {
 465       _.each(messageSystems[system], function(method) {
 466         this[system][method] = proxyMethod(this, system, method);
 467       }, this);
 468     }, this);
 469   }

Any advice?

Erastes answered 6/2, 2016 at 2:7 Comment(6)
Hmm, interesting. It is not the case that messageSystems is undefined so that line 465 (messageSystems[system]) is failing?Lipinski
No, its odd. Using the debugger, its def this that is undefined. It is trying to resolve... this["vent"]["on"]Erastes
I thought it might be something do with the nested each, both passing in this as the context - but I mocked up the same function above and it works fineErastes
Hmm, lodash no longer has the context as the third param in .each... ` function forEach(collection, iteratee) { return baseEach(collection, toFunction(iteratee)); }` So the context is lost in the aboveErastes
Yeah, that is it, lodash 3.10.1 works fine - its the lack of context in the newer lodash that marionette is not compatible withErastes
Nice find! Maybe you should add an answer? I bet more people would like to know :)Lipinski
E
9

For anyone else looking at this, the answer is no.

Lodash 3.10.1 is fine, but the 4.x release has removed the context option from many of the functions, which breaks Marionette.

The old way was

    _.each(collection, iteratee, context);

The new way is

_.each(collection, _.bind(iteratee, context));

But so far so good with using 3.10.1 with the above requireJS set up.

So until Marionette is updated, you have to hold off on 4.x

Erastes answered 6/2, 2016 at 16:35 Comment(2)
Do you find a noticeable difference in speed since the switch? :)Lipinski
@Lipinski No, no noticeable difference in speedErastes

© 2022 - 2024 — McMap. All rights reserved.