【问题标题】:swap underscore 1.8.3 for lodash 4.2.1 in backbone marionette 2.4.4在骨干木偶 2.4.4 中将下划线 1.8.3 替换为 lodash 4.2.1
【发布时间】:2016-02-06 02:07:31
【问题描述】:

这甚至可能吗?继续阅读关于此的相互矛盾的报告。

我有一个 Marionette 应用,刚刚升级到 2.4.4。

如果我用 lodash 代替下划线 - 使用 requireJS,

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

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

我收到以下错误...

Uncaught TypeError: Cannot read property 'vent' of undefined

lodash 加载正常,只是木偶在抱怨。

看来466 线上的上下文this 是未定义的

 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   }

有什么建议吗?

【问题讨论】:

  • 嗯,很有趣。 messageSystems 不是 undefined,所以第 465 行(messageSystems[system])失败了?
  • 不,这很奇怪。使用调试器,它的 def this 是未定义的。它正在尝试解决... this["vent"]["on"]
  • 我认为这可能与嵌套的每个有关,两者都传入 this 作为上下文 - 但我模拟了上面相同的函数,它工作正常
  • 嗯,lodash 不再将context 作为.each 中的第三个参数...` function forEach(collection, iteratee) { return baseEach(collection, toFunction(iteratee)); }` 所以上下文在上面丢失了
  • 是的,就是这样,lodash 3.10.1 工作正常 - 它在新的 lodash 中缺少上下文,木偶不兼容

标签: javascript backbone.js underscore.js marionette lodash


【解决方案1】:

对于其他人来说,答案是否定的。

Lodash 3.10.1 很好,但 4.x 版本已从许多函数中删除了 context 选项,这会破坏 Marionette

以前的方法是

    _.each(collection, iteratee, context);

新方法是

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

但到目前为止,在上述 requireJS 设置下使用 3.10.1 效果很好。

所以在Marionette 更新之前,您必须推迟4.x

【讨论】:

  • 您是否发现切换后的速度有明显差异? :)
  • @Tholle 不,速度没有明显差异
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 2017-02-13
相关资源
最近更新 更多