Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LoDash with EmberCLI

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):

  1. bower install lodash --save
  2. 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:

  1. bower install d3 --save
  2. In Brocfile.js, have this line: app.import('bower_components/d3/d3.js');

And variable named 'd3' is automatically available.


Added related link:

  1. Import custom library in ember-cli
  2. 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.)
  3. Proper way to access third party libs such as D3 in Ember CLI?
like image 311
Cokorda Raka Avatar asked Jan 27 '15 01:01

Cokorda Raka


1 Answers

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 :)

like image 192
andrusieczko Avatar answered Oct 04 '22 05:10

andrusieczko