ember-cli: creating a helper that would render a view? - ember.js

I'm trying to reproduce Ember-TodoMVC with ember-cli. I'm stuck with this part.
I've created a view like this:
app/views/action-edit.coffee
ActionEditView = Ember.TextField.extend
didInsertElement: -> #$().focus()
`export default ActionEditView`
When i use it in an Emblem template directly, e. g. view "action-view", it works fine: a text field is rendered.
But emberjs.com/guides suggests creating a helper to render the view.
I found this remark: "Remember that you must register your helpers by exporting makeBoundHelper" on ember-cli website. After fiddling for a while struggling to understand how ES6 modules work, i ended up with this code that does not produce any JS errors:
app/helpers/action-edit.coffee
`import ActionEditView from 'loltodo/views/action-edit'`
`export default Ember.Handlebars.makeBoundHelper(ActionEditView)`
When i use it like this in an Emblem template: action-edit, Ember outputs this in browser console:
[✓] helper:action-edit ......................................... loltodo/helpers/action-edit vendor/ember/ember.js:3521
So i assume the helper gets hooked up fine.
The problem is that it renders blank!
I also tried this:
app/helpers/action-edit.coffee
`import ActionEditView from 'loltodo/views/action-edit'`
`export default Ember.Handlebars.helper('action-edit', ActionEditView)`
It results in error "undefined is not a function" in this line.
So the question is: how do i create a helper that render a view with ember-cli to reproduce this step of the Ember-TodoMVC tutorial?

Like Stefan says: the docs describe this so here are the steps:
from command prompt run ember generate helper "luis-highlight"
make sure your helper name has a dash.. ember-cli does not want
conflict with html tags (if no dash then it does not work).
inside helpers/luis-hightlight.js write this:
import Ember from 'ember';
export default Ember.Handlebars.makeBoundHelper(function(value) {
return new Ember.Handlebars.SafeString('<span class="hightlitht">' + value + '</span>');
});
call helper from template:
{{luis-hightlight 'embercli is great'}}

consider looking at: https://github.com/WMeldon/ember-cli-todos/blob/master/app/components/edit-todo.js it should have an idiomatic ember-cli todo setup

Related

cannot load a custom helper

I have wrote a highlight helper follow the ember guides.
app/helpers/highlight.js
export default Ember.Handlebars.makeBoundHelper( function(value, options) {
var escaped = Handlebars.Utils.escapeExpression(value);
return new Ember.Handlebars.SafeString('<span class="highlight">' + escaped + '</span>');
});
I invoke the helper in the application template with {{highlight name}} and declare the name in application controller . When visit the index page I got this error
Assertion Failed: A helper named 'highlight' could not be found. Seems the helper is not loaded. Is there any configuration to load the helper ?
I assume that you use ember-cli, as you tagged it this way.
Plain ember and ember-cli are using different resolvers (basiclly mechanisms that search for files in proper directories) and thus have a little bit different name conventions. Helpers in ember-cli must have a dash in their name.
Take notice that if you only put the code you mentioned in your question, this file will have no information what is Ember. You still need to import Ember using modules. Very nice introduction can be found here.
To sum up, change your helper file name to one that includes a dash and the helper will be recognized across the environment.

How do I get ember-qunit to use my ApplicationAdapter instead of DS.FixtureAdapter

I'm trying to use http-mock as suggested by the ember-cli documentation instead of fixtures. I generated a mock and can verify (using curl) that my ember app serves it.
However, in testing, the adapter is set to DS.FixtureAdapter and I think this is the source of the problem. If I could do something like the following:
container.register('adapter:application', ApplicationAdapter)
I think it moduleForModel() would find my adapter, where I would import ApplicationAdapter from the adapter I want to use:
import ApplicationAdapter from 'myapp/adapters/application'
But I don't know where I can put this code. I tried hooking into the delegate callback, but it wasn't getting called by moduleForModel().
Ideas about how this is supposed to be done?
I was having the exact same problem, and thanks to the comments here I figured it out. :) Add the adapter to the needs property in the test:
moduleForModel('post', 'Post', {
needs: ['adapter:application', ... ]
});

Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

I'm trying to use moment.js in my ember.js app (built with ember-cli), I have a trouble with this error
Handlebars error: Could not find property 'formatDate' on object
I think it's same as this error How to use Custom helpers in ember-app-kit? but I already did the same approach but not working yet. Anyone got same error? Please help me to figure out.
I put
app.import('vendor/momentjs/moment.js'); in Brocfile.js
and
"moment": true in .jshintrc as in ember-cli documentation,
and I used the helper {{formatDate date}} in PostsTemplate
I created a helper app/helpers/formatDate.js
var formatDate = Ember.Handlebars.makeBoundHelper(function(date) {
return moment(date).fromNow();
});
export default formatDate;
I also tried this syntax in app/helpers/formatDate.js, but neither works and both get same error
export default Ember.Handlebars.registerBoundHelper('formatDate',function(date) {
return moment(date).fromNow();
});
I think your file name 'formatDate.js' has the wrong format. Try 'format-date.js' and it should work.
Excerpt from http://iamstef.net/ember-cli/:
Handlebars helpers will only be found automatically by the resolver if their name contains a dash (reverse-word, translate-text, etc.) This is the result of a choice that was made in Ember, to help both disambiguate properties from helpers, and to mitigate the performance hit of helper resolution for all bindings.
Use your new 'format-date' helper like this:
{{format-date "29/05/2014"}}
I ran into this symptom as well and had a different solution.
I had a helper in app/helpers/fh.js called 'fh' in order to be able to use it I needed to add it to the controller as follows
import fh from '../helpers/fh';
If I didn't have the import line I would get the following error:
"Handlebars error: Could not find property 'fh' on object"

ember js how to remove the hash tag in my url

I have seen an answer to this question and was directed to the Ember API Docs for using the browser's history.pushState ability
Apparently I need to add this code to my router.js file
App.Router.reopen({
location: 'auto'
});
However, doing so breaks my app! It is a very simple app so far, since I am still only learning... so its basically just a default installation with only 4 templates, 4 routes. I am using Ember App Kit which, I noticed initializes the router slightly differently than the ember guides describes.
Is there something different I need to do? or is there something I am doing wrong in general?
Ok... I found the answer, for anyone who might run into this same issue.
Ember app kit seems to define the router in a variable just called Router, so I don't need to use the conventional naming requirements.
All that needs to be added to the router.js is this:
Router.reopen({
location: 'auto'
});
:D

using an ember-rails handlebars template outside the context of an ember view

I have a rails app that is using the ember-rails gem.
There is a section of my site that is not on ember, but where it would be convenient to use one of the handlebar templates served via the asset pipeline. However, something seems to be going wrong. Specifically, my template is returned like so:
Ember.TEMPLATES["views/wanderlists/templates/gallery"] = Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) { helpers = helpers || Ember.Handlebars.helpers; var self=this;
data.buffer.push("<h1>Gallery!</h2>\n"); });
However, if I try to use this template:
Ember.TEMPLATES["views/wanderlists/templates/gallery"]({})
TypeError: Cannot read property 'buffer' of undefined
Any idea why the generated template would be having trouble?
Any idea why the generated template would be having trouble?
You can't call handlebars templates compiled by the ember handlebars compiler as if they were normal handlebars templates. They expect a completely different set of arguments. Specifically, they expect to be passed (context, options) where options has a data.buffer that output will be written to. So for example, if you try:
Ember.TEMPLATES["views/wanderlists/templates/gallery"](this, {data: {buffer: 'NOT-A-BUFFER'}})
console should output TypeError: Object NOT-A-BUFFER has no method 'push'
There is a section of my site that is not on ember, but where it would be convenient to use one of the handlebar templates served via the asset pipeline.
OK. This is really easy to do, just not by accessing Ember.TEMPLATES directly. Instead use an Ember.View, and call appendTo() directly to render. For example:
App = Ember.Application.create({});
var view = Ember.View.create({
templateName: "views/wanderlists/templates/gallery",
name: "Bob"
});
view.appendTo("#message");
Working example here: http://jsfiddle.net/mgrassotti/VWmFq/1/
For more details see Ember Guides: Defining a view