使用LoDash与EmberCLI [英] Using LoDash with EmberCLI
问题描述
我没有看到任何网络上的线索/文章, ,如何做到这一点的分步说明。
如果可能,使用lodash v3.0.0(我正在使用最新的ember-cli,v0.1.9 )
谢谢,
Raka
我发现如何,你需要生成一个定制的lodash(lodash现代)。使用lodash-cli: https://lodash.com/custom-builds
在命令控制台上键入:lodash modern ...,您将获得一个生成的javascript文件:lodash.custom.js
将该文件放在您的ember-cli项目的供应商目录下。
修改Brocfile,添加以下内容:
app.import('vendor / lodash.custom.js',{
'lodash':[
'default'
]
});
这就是....你不必做import _ from'lodash' 你的任何js文件。事实上,不要这样做(你会得到一个错误)。 _ var可以很容易地使用。
例如,我有一个这样的Route对象:
从ember导入Ember;
export default Ember.Route.extend({
model:function(){
console.log('hohoho:'+ _.chunk(['a',' b','c','d'],2));
返回Ember.Object.create({name:'Raka'});
}
});
我可以看到hohoho:,b,c,d被打印在javascript控制台当我访问路线。
CORRECTION
你真的不需要那个lodash-cli。
我已经尝试过这样(我想更合适):</ p>
- bower install lodash --save
- 在Brocfile.js中,具有以下行:app.import('bower_components / lodash / lodash.js');
就是这样。 _在路由器/控制器中自动提供。
我和d3一样:
- bower install d3 --save
- 在Brocfile.js中,具有以下行:app.import('bower_components / d3 / d3.js');
变量名为'd3'可自动使用。
添加相关链接:
- http://tiku.io/questions/2866484/how-to-include-a-local-javascript- im-ember-cli-application (引用:如果您不需要它们在vendor.js文件中缩小,可以将它们放在public / js中,然后将其作为普通脚本文件包含在app / index.html。我使用这种方法为一些图书馆,如moment.js。公开文件夹在构建期间直接复制到您的站点根目录。)
- 正确的方法来访问第三方库,如Ember CLI中的D3?
您可以使用一些准备工作:
https://github.com/levanto-financial/ember-lodash
或手动执行。
我没有任何示例,但是应该像修改这3个文件一样简单:
bower.json
只需添加
lodash:4.16.4,
到您的依赖项
并运行 bower install
在您的命令行。
或者,您可以通过 bower
安装它:
$ bower install lodash --save
Brocfile.js
为了被包括来源:西兰花:
app.import('bower_components / lodash / lodash.js');
添加到 var app = new EmberApp();
.jshint.rc
添加行:
_:true,
在 predef
部分的某个地方(如果您不想看到像 _未定义的警告
)。
我没有测试过,但我希望它有帮助:)
Does anyone have a working example of a (simple) ember-app project built using Ember-CLI that uses LoDash? (e.g.: I want to use lodash, _.someLodashFunc, in my Routes and Controllers).
I haven't seen any thread / article on the web that gives clear, step-by-step explanation on how to do that.
If possible with lodash v3.0.0 (and I'm using the latest ember-cli, v0.1.9).
Thanks, Raka
I found out how, you need to generate a custom build of lodash (the "lodash modern"). Use lodash-cli: https://lodash.com/custom-builds
On the command console type: lodash modern ..., and you'll get a javascript file generated: lodash.custom.js
Put that file under "vendor" dir of your ember-cli project.
Modify the Brocfile, add this:
app.import('vendor/lodash.custom.js', {
'lodash': [
'default'
]
});
And that's it.... you don't have to do "import _ from 'lodash'" in any of your js files. In fact, don't do that (you'll get an error). The _ var is readily available.
For example, I have a Route object like this:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
console.log('hohoho: ' + _.chunk(['a', 'b', 'c', 'd'], 2));
return Ember.Object.create({name: 'Raka'});
}
});
And I could see hohoho:,b,c,d got printed in javascript console when I visited that route.
CORRECTION
You don't really need that lodash-cli.
I've tried this way (I guess more proper):
- bower install lodash --save
- In Brocfile.js, have this line: app.import('bower_components/lodash/lodash.js');
That's it. _ is automatically available in your Routers / Controllers.
I did the same with d3:
- bower install d3 --save
- In Brocfile.js, have this line: app.import('bower_components/d3/d3.js');
And variable named 'd3' is automatically available.
Added related link:
- Import custom library in ember-cli
- http://tiku.io/questions/2866484/how-to-include-a-local-javascript-im-ember-cli-application (quote: If you don't need them to minified in your vendor.js file you can put them in the public/js and then include it as a normal script file in app/index.html. I use this method for some libraries like moment.js. The public folder gets directly copied to your site root during the build.)
- Proper way to access third party libs such as D3 in Ember CLI?
You can use something ready: https://github.com/levanto-financial/ember-lodash or do it manually.
I don't have any example but it should be as easy as modifying these 3 files:
bower.json
Just add the line
"lodash": "4.16.4",
to your dependencies
and run bower install
in your command line.
Alternatively, you can install it via bower
:
$ bower install lodash --save
Brocfile.js
In order to be included to sources by Broccoli:
app.import('bower_components/lodash/lodash.js');
add this somewhere after var app = new EmberApp();
.jshint.rc
Add the line:
"_": true,
somewhere in the predef
section (if you don't want to see the warnings like _ is undefined
).
I haven't tested that but I hope it helps :)
这篇关于使用LoDash与EmberCLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!