Can't get template portion to render in Ember.js - ember.js

I am having some issues getting Ember.js to pick up and render a handlebars template file. I get the following error in my browser's debug console:
Error: <(subclass of Welcome.LoginView):ember166> - Unable to find template "login_view".
I have the following code in emberjs code in my app.js file:
Welcome = Ember.Application.create();
Welcome.LoginView = Ember.View.extend({
templateName: 'login_view'
});
I also have the following snippet in my index.html file:
<script type="text/x-handlebars">
{{view Welcome.LoginView}}
</script>
Finally, relative to the app.js file I have the file templates/login_view.handlebars, which contains the template contents to render:
<form class="form-inline">
<fieldset>
<input type="text" name="email" placeholder="email address">
<input type="password" name="password" placeholder="password">
<button class="btn btn-primary" type="submit">Sign In</button>
<!-- ... -->
</fieldset>
</form>
The error seems to indicate that it can't locate the handlebars template. When I look at the generated HTML I don't see the above form on the page.
Can anyone shed some light on what I am doing wrong?

From your question I assume that you are NOT using some tools which precompile a handlebars template for you. Ember looks for templates in the Ember.TEMPLATES array–it does not automatically look for templates located at templates/ and ending with .handlebars. Without a build step that compiles the templates for you, you have to do this on your own.
This may sound complicated but it's as easy as the following:
Ember.TEMPLATES["login_view"] = Ember.Handlebars.compile('TEMPLATE CODE');
You have to make sure that this template definition occurs before you want to use it.
Another solution would be to inline your template into your index.html and add a data-template-name="login_view" attribute to the script tag:
<script type="text/x-handlebars" data-template-name="login_view">
<form class="form-inline">
<!-- ... -->
</form>
</script>
However, if your project has numerous templates then I would suggest you adopt a build tool that does the first option for you.
Related question: Is it possible to load a Handlebars template via Ajax?

Related

Ember - Compile Error: bs-form-element is not a helper

Recently i update my existing ember ember project to 2.10.0 version after update i try to run the project but it shows some compile error
uncaught Error: Compile Error: bs-form-element is not a helper
I include this in login page on my project like this
<div class="panel-body">
{{#bs-form formLayout="vertical" model=this action="loginAction" class="form-signin"}}
<fieldset>
{{bs-form-element controlType="text" placeholder="Bank ID" property="userid" value=userid elementId="userid" required="required" autofocus="autofocus" style="text-align:left" maxlength="7"}}
{{bs-form-element controlType="password" placeholder="Password" property="password" value=password elementId="password" required="required" style="text-align:left" maxlength="10"}}
<!--div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div-->
{{bs-button defaultText="Login" class="btn btn-lg btn-primary btn-block" buttonType="submit" }}
</fieldset>
{{/bs-form}}
</div>
I am not sure whether this is plugin related issue or something could some one please help to sort this issue
Ember throws this error if there is no component or helper with the given name found in your project or your dependent addon's.
Check your package.json and the version of ember-bootstrap. I think your app used pre 1.0, because bs-form-element is old api.
Maybe the addon was updated accidentally to >= 1.0, while updating ember.
Another possible oversight to check for, specific to ember-bootstrap and black/white lists, is accidentally including or excluding the needed component from the black/white lists. For example, if you're using a whitelist, make sure the needed component is referenced in it:
// ember-cli-build.js
'ember-bootstrap': {
'whitelist': [
'bs-form'
],
}

Passing variables to handlebars partials in Ember.js

What I want to do should be fairly simple.
I want to pass variables to partials for reusability.
I want to do something like this :
<form {{action login content on="submit"}}>
<fieldset>
{{partial 'components/field-email' label="Email" fieldname="email" size="full"}}
[...]
</fieldset>
</form>
Instead of doing this :
<form {{action login content on="submit"}}>
<fieldset>
<div {{bind-attr class=":field :email size"}}>
<label {{bind-attr for=fieldname}}>{{label}}</label>
{{input type="email" id=fieldname name=fieldname valueBinding="email" placeholder=label}}
</div>
[...]
</fieldset>
</form>
coming from Rails, I expected this to just work, but it seems I can't (don't know how to) pass variables to a partial. I looked at all the ways to "include a template part":
partial
view
render
The thing that worked for me is using a View. But I thinks it's overkill. I just want separate sub-templates for reusability and readability, no context change or specifying a controller needed here.
Edit:
I also tried to use this partial as a component :
{{field-email type="email" id="email" name="email" valueBinding="email" placeholder=label size="full"}}
Which works for everything except the valueBinding.
I guess it's also worth mentioning that I have a route setup with an action that calls login on my AuthController :
App.LoginRoute = Ember.Route.extend
model: -> Ember.Object.create()
setupController: (controller, model) ->
controller.set 'content', model
controller.set "errorMsg", ""
actions:
login: ->
log.info "Logging in..."
#controllerFor("auth").login #
This whole thing works if all the markup is in the login template but fails if I try to break it up with partials, components and such.
There has to be something that I didn't see...
You should use a component in this case.
If you setup your template correctly (components/field-email), you can use on this way:
{{field-email label="Email" fieldname="email" size="full"}}
You could setup the html component properties, if you define the component. Based on your example, it could be:
App.FieldEmailComponent = Ember.Component.extend({
classNames: ['size'],
classNameBindings: ['email', 'field'],
field: null,
email: null
});
Example: http://emberjs.jsbin.com/hisug/1/edit
Got it working, I had to use a component. I had messed up the "value" part.
components/field-email.hbs :
<div {{bind-attr class=":field :email size"}}>
<label {{bind-attr for=fieldname}}>{{label}}</label>
{{input type="email" name=fieldname value=value placeholder=label}}
</div>
login.hbs :
<form {{action login content on="submit"}}>
<fieldset>
{{field-email label="Email" fieldname="email" value=email size="full"}}
[...]
</fieldset>
</form>
What I get from this is that in order for attributes to be used in a component they have to be explicitly set when using the component. Once they are set, they are bound.
In my case, when the input value changes, the associated route property is updated as well which is pretty cool.

Ember-data how to bind a file upload to model

There are bunch of answer on how to jQuery upload. That's not what I want. I want to simply bind the "file" input so that it's send with my object when I submit the form.
App.Document = DS.Model.extend({
document_name: DS.attr(),
document_file: DS.attr()
});
<form role="form" {{action save on="submit"}}>
<div class="thumbnail" {{action 'start'}}>
<img {{bindAttr src=src}} class="preview"/>
<img class="shadow hide"/>
<canvas class="hide"></canvas>
</div>
{{input type="file" valueBinding="document_file" name="document_file" }}
{{input type="text" valueBinding="document_name" name="document_name"}}
<div>
<button class="btn btn-primary" {{action 'save'}}>Save</button>
</div>
</form>
I haven't found a single tutorial on simple upload. It can't be too hard to send a file right?
It is actually pretty simple to do it althougt it is not functionality out of the box. See my question here with working example: Ember.js value binding with HTML5 file upload
Ember Data doesn't support it out of the box, you'll need to override the adapter and implement your own version of the createRecord/updateRecord which modifies the ajax call. It's probably easier to just use jquery.

How to use autofocus with ember.js templates?

I have a simple ember.js text field and I'm trying to add autofocus
{{view PersonApp.SearchField placeholder="search..." valueBinding="searchText"}}
PersonApp.SearchField = Ember.TextField.extend({
});
Can I add this in the javascript or is at as simple as a attribute in the template itself?
Update:
More recent versions of Ember now have support for this built in, so you no longer need to reopen TextField to add an attributeBinding. As of January 2014 (commit fdfe8495), you can simply use the HTML5 autofocus attribute in your template:
{{input value=search type="text" placeholder="Search" autofocus="autofocus"}}
Here is a simple jsfiddle demonstration.
Previous Solution:
You can also reopen TextField to allow you to bind the autofocus attribute:
Ember.TextField.reopen({
attributeBindings: ['autofocus']
});
And then in your template:
{{input value=search type="text" placeholder="Search" autofocus="autofocus"}}
There is also the option to use the HTML5 autofocus attribute on the TextField.
PersonApp.SearchField = Ember.TextField.extend({
attributeBindings: ['autofocus'],
autofocus: 'autofocus'
});
See also documentation on Mozilla Developer Network for further information about the autofocus field:
Autofocus meaning that we start focusing on the text box right away? You want didInsertElement for that.
didInsertElement: function() {
this.$().focus();
}
http://jsfiddle.net/qKXJt/139/
I wrapped this method in a little 1kb package to solve this even a bit more elegantly, directly in the template, without any further coding:
<body>
<!-- all the libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.2.0/ember.min.js"></script>
<script src="http://rawgithub.com/AndreasPizsa/ember-autofocus/master/dist/ember-autofocus.min.js"></script>
<!-- your template -->
<script type="text/x-handlebars">
Hello, world! {{ input }}
:
: more elements here
:
{{ autofocus }}
</script>
<!-- your app -->
<script>
Ember.Application.create();
</script>
</body>
The package is at https://github.com/AndreasPizsa/ember-autofocus
(or on bower install ember-autofocus). Enjoy!

Using Ember.js with Handlebars.js View in Jade

I am trying to add an ember view using handlebars.js in jade. When I use this code
script(type='text/x-handlebars')
{{view App.LoginView}}
{{view Ember.TextField valueBinding="username" placeholder="Enter your username"}}
it renders it as:
<div id="ember194" class="ember-view"></div>
<input id="ember204" class="ember-view ember-text-field" placeholder="Enter your username" type="text">
I can't seem to get the textfield to be wrapped within the view. I am wondering if there is a trick to be able to use handlebars correctly within a jade template.
The desired result that I want is:
<div id="ember194" class="ember-view">
<input id="ember204" class="ember-view ember-text-field" placeholder="Enter your username" type="text">
</div>
Try:
script(type='text/x-handlebars')
{{#view App.LoginView}}
{{view Ember.TextField valueBinding="username" placeholder="Enter your username"}}
{{/view}}
Jade saves you from HTML closing tags but the handlebars block must be intact, and what you're asking for requires the Ember.TextField to be inside a {{#view}} block.
[Edit] FYI check out http://emblemjs.com/