Skip to content
This repository has been archived by the owner on Feb 6, 2021. It is now read-only.

How to use this in combination with coffeescript? #17

Closed
varblob opened this issue Jul 10, 2015 · 6 comments
Closed

How to use this in combination with coffeescript? #17

varblob opened this issue Jul 10, 2015 · 6 comments

Comments

@varblob
Copy link

varblob commented Jul 10, 2015

Back ticks escaping to pure js in ember-cli-coffeescript. Any suggestions on how to get past this.

@pangratz
Copy link
Contributor

Sorry, my Coffeescript skills are very limited so I don't really know if there is a way. I tried using embedded JavaScript functionality but this doesn't work since the backtick is used for escaping ...

One possible way I could think of is defining the templates in an ES6 file and import them in your Coffeescript, but the advantage of seeing the template in the test is lost. Something like the following (untested):

// tests/templates.js
import hbs from 'htmlbars-inline-precompile';

export var testTemplate = hbs`
  {{#my-component}}
    hello
  {{/my-component}}
`;
// tests/unit/component/my-component.coffee
import { testTemplate } from '../../../templates';

test "it works", (assert) ->
  @render testTemplate

  assert.equals @$().html().text().trim(), "hello"

@varblob
Copy link
Author

varblob commented Jul 10, 2015

I think your idea should work but as you stated it makes the tests considerably less readable. It's too bad there isn't a way to escape backticks so I could do something like hbs\{{#my-component}}hello{{/my-component}}``. I'm gonna see if maybe coffeescript has something to say about escaping backticks. Looks like a known bug and no action to repair jashkenas/coffeescript#1504 (comment)

@pangratz
Copy link
Contributor

I have another idea: hbs('template string') will be precompiled by ember-cli-htmlbars-inline-precompile as well (not just tagged template strings, like currently). This allows you to write tests in Coffeescript like this:

`import hbs from 'ember-cli-htmlbars-inline-precompile';`

test "it works", (assert) ->
  @render '
    {{#my-component}}
      hello
    {{/my-component}}
  '

  assert.equals @$().html().text().trim(), "hello"

@pangratz
Copy link
Contributor

I released v0.2.0 which adds support for passing a single string to the hbs() function. See tests/unit/components/coffeescript-support-test.coffee and tests/unit/views/precompile-test.js on how this is used. I also added a section to the README#CoffeeScript.

Thanks for opening the issue and giving me the idea to enhance this addon! 🍻

I am going to close this issue. Feel free to comment if you have any issues. Cheers!

@varblob
Copy link
Author

varblob commented Jul 11, 2015

Works a charm, if you're ever in NY I owe you a beer!

@pangratz
Copy link
Contributor

Great, glad it works. Happy testing!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants