【问题标题】:How to properly manage underscore mixins via meteor?如何通过流星正确管理下划线混合?
【发布时间】:2015-07-06 12:03:59
【问题描述】:

我想用lodash和underscore.string,但项目中还需要下划线。下划线seems to have a method called _.mixin to handle this operation

我将 mixins 放在我的 lib/mixins.js 目录中,以便首先加载它。

_.mixin(lodash);       // loads lodash
_.mixin(s.exports());  // loads underscore.string

但是,当我打开 meteor shell 并执行 console.dir(lodash)console.dir(_) 时,我希望下划线包含与 lodash 相同的所有方法,但事实并非如此。它似乎只是简单的下划线。

meteor 如何管理 mixin?

【问题讨论】:

  • 我确认,下划线“_”似乎在某处被覆盖,我们失去了所有混合功能。按照 Tom 的建议将它们放入 lib 文件夹并不能解决任何问题。这可能是一个错误(或竞争条件?)
  • 可能跟shell有关

标签: javascript meteor lodash


【解决方案1】:

我没有尝试使用 lodash,但经常使用 mixins。

正如你已经做的那样,我也将它们放在了lib/mixins.js

我的包含例如以下代码

// extended functions
function isDefined(obj) {
  return obj !== void 0;
};

// bring them into scope of underscore
_.mixin({ 'isDefined': isDefined })

这不是依靠流星而是下划线。

我想,lodash 不会返回有效的对象列表。

应该是这样的:

_.mixin({ 'name': function, ... });

对于我猜你应该调用的字符串

_.mixin(_s.exports());

这可能是一个非常有用的来源:http://blog.falafel.com/the-lo-dash-on-underscorejs-and-strings/

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,在某些时候意外丢失了 mixin。 因此我安装了lodash 或下划线

    meteor add erasaur:meteor-lodash
    

    然后我将我的 mixin 描述文件放在某处(在我的情况下为/lib/moreUnderscore.js),其中包含我的函数

    lodash.mixin({ 'something': function(){ ... } })
    

    当然用法是lodash.something而不是_.something

    【讨论】:

      猜你喜欢
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多