1tree-factory 中文文档教程

发布于 8年前 浏览 31 项目主页 更新于 3年前

1tree-factory

基本上只是 1tree 上的一个薄外观,用于创建一个新的用例 当你有自己的适配器/插件等时,1tree 实例更容易

。1tree 导出一个默认树,只要你想要就可以开始使用 使用1tree默认的底层数据结构,同时也暴露了一些方法 用于创建使用您自己的适配器和/或插件的树。

大多数人只想开始使用树,1tree 的默认导出假设 这。

但是,如果你想使用你自己的适配器,或者使用你自己的插件也 使用 1tree 的基本插件,有一些摆弄。 不难 但这样做往往很快就会变老。

此模块还插入指定的插件 before 1tree 的基本插件 - 这是为了确保各种 1tree 插件可以做类似包装其他的事情 插件或以某种方式扩展它们将与您的插件很好地配合使用。 如果你 想要添加您的插件之后添加了基本插件,您可以 始终在树实例上调用 plugin 方法。

usage

/*
  we use the name Mtree (mojule tree) internally but the npm package name was
  taken
*/
const Mtree = require( '1tree-factory' )

/*
 create a tree using the default adapter but with some extra plugins that are
 added before the base plugins
*/
const tree1 = Mtree( plugin1, plugin2, plugin3 )

// same, but array instead of multiple args
const tree2 = Mtree( [ plugin1, plugin2, plugin3 ] )

// same, but with a custom adapter
const tree3 = Mtree( adapter, plugin1, plugin2, plugin3 )

// pass a boolean with true to exclude the base plugins
const tree4 = Mtree( plugin1, plugin2, plugin3, true )
const tree5 = Mtree( adapter, plugin1, plugin2, plugin3, true )

// add plugins after the fact, for example to wrap or extend existing plugins
tree5.plugin( plugin4 )

// same as just using 1tree
const tree6 = Mtree()

1tree-factory

Basically just a thin facade over 1tree to make the use case of creating a new 1tree instance easier when you have your own adapter/plugins etc.

1tree exports a default tree that's ready to start using provided that you want to use 1tree's default underlying data structures, and also exposes some methods for creating trees that use your own adapter and/or plugins.

Most people just want to start using a tree, 1tree's default export assumes this.

However, if you want to use your own adapter, or use your own plugins but also use 1tree's base plugins, there's a bit of fiddling around. It's not difficult but doing it often gets old fast.

This module also inserts the plugins specified before 1tree's base plugins - this is to ensure that the various 1tree plugins that do things like wrap other plugins or extend them in some way will play nicely with your plugins. If you want to add your plugins after the base plugins have been added, you can always call the plugin method on the tree instance.

usage

/*
  we use the name Mtree (mojule tree) internally but the npm package name was
  taken
*/
const Mtree = require( '1tree-factory' )

/*
 create a tree using the default adapter but with some extra plugins that are
 added before the base plugins
*/
const tree1 = Mtree( plugin1, plugin2, plugin3 )

// same, but array instead of multiple args
const tree2 = Mtree( [ plugin1, plugin2, plugin3 ] )

// same, but with a custom adapter
const tree3 = Mtree( adapter, plugin1, plugin2, plugin3 )

// pass a boolean with true to exclude the base plugins
const tree4 = Mtree( plugin1, plugin2, plugin3, true )
const tree5 = Mtree( adapter, plugin1, plugin2, plugin3, true )

// add plugins after the fact, for example to wrap or extend existing plugins
tree5.plugin( plugin4 )

// same as just using 1tree
const tree6 = Mtree()
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文