Cameron
Cameron

Reputation: 2965

How can I include a pure javascript file into our ember application?

Our team is working on a new website that is going to use Algolia as our search engine. The problem is that there isn't an ember module that exists to use it (they are all old or unfinished).

Therefore, in order to use Algolia, we need to include a plain javascript file into our application. What is the best way to do this? We found that it works if we include the script into our index.html.

It looks something like this

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    {{content-for "head"}}

    {{content-for "head-footer"}}

  </head>
  <body>
    {{content-for "body"}}

    <!-- Include Scripts Here -->
    <script src="algolia.js"></script>


    {{content-for "body-footer"}}
  </body>
</html>

We are putting a javascript file into our index.html. While this seems to work I'm a little suspicious that this is not an ember solution to the problem. Are there other ways to include external javascript files to use throughout our program? Or is this solution fine as it is?

Upvotes: 1

Views: 743

Answers (1)

ykaragol
ykaragol

Reputation: 6221

In Managing Dependencies section of the ember guide, a strategy is given.

We use nearly the same strategy in our projects:

  1. Search for a suitable ember addon. If any addon is found and the addon is ok (up-to-date, clear-to-use, not-buggy), prefer that addon.
  2. If there is no suitable ember addon, search for bower or npm package. Using npm package is a little bit harder than using bower package.
  3. If no suitable distribution, just copy the js file to the vendor directory and use it by importing with app.import at ember-cli-build file. Ref

Actually, we generally don't prefer modifing the index.html.

Upvotes: 3

Related Questions