ember version 1.6.1 best to way to bind css style - ember.js

I am using ember version 1.6.1. I would like to show an error message if user doest not enter username and password correctly. I think i have to use bind-style. now I have code like this:
<fieldset class="error-message" {{bind-style visibility="isVisible:visible:hidden"}}>
<span>invalid username/password</span>
</fieldset>
what is the best way to do it ?

Ember Handlebars supports dynamic class binding exceptionally better than it does style binding. To do that you'd bind-attr to the class. http://emberjs.com/guides/templates/binding-element-class-names/
Css
.visible{
visibility:visible;
}
.hidden{
visibility:hidden;
}
Handlebars
<fieldset {{bind-attr class=":error-message isVisible:visible:hidden"}}>
<span>invalid username/password</span>
</fieldset>
Example: http://emberjs.jsbin.com/didax/1/edit
You can bind-attr the style property and create a computed property that returns the raw style text visibility:visible, but that's ugly and not necessary in this situation.

Although class is generally the best way to set these visual changes, consider using classNameBindings instead of bind-attr. That would require you to create a View class.
However, the best way to bind element attributes that don't have a specific binding mechanism, would be via attributeBindings:
(this approach also needs a View class)
App.IndexView = Ember.View.extend({
attributeBindings: ['style'],
style: function() {
return 'color: #F00';
}.property()
});
This is way is a little better because you can watch the style property of your view class and it will automatically bind to your view markup. And since that is a computed property, you can create your own code to determine changes of other attributes in your view that could cause the style attribute to be reconstructed, and again, automatically bound to your view.
You could have a property that the style property watches with property('dependency'), so when it changes, style is once again computed and the view is updated. For example, let's say that you have a view which is a custom input box with built-in validation. You have a property valid which returns boolean, being true for valid and false for invalid values.
App.IndexView = Ember.View.extend({
attributeBindings: ['style'],
valid: function() {
return false;
}.property(),
style: function() {
// these variables and all should ideally be somewhere else,
// as color codes could potentially be global for the app
var _invalidColor = "#F00";
var _validColor= "#000";
if (this.get('valid')) {
return 'color: ' + _validColor + ';';
} else {
return 'color: ' + _invalidColor + ';';
}
}.property('valid')
});
(see jsbin)
Keep in mind this is a crude example to show the functionality/possibilities. Manually change the return value of valid property of the IndexView in JS Bin to see how it affects the view template.

Related

Ember 2.3 how to use itemControllers in each loops?

Before anyone brings up components, I must state that I am aware that Ember is moving away from controllers and views completely and adopting the component structure. Right now, I am compelled to use controller/view in ember2.3 using the legacy-controller and legacy-view addons that have been provided here:
https://github.com/emberjs/ember-legacy-controllers
https://github.com/emberjs/ember-legacy-views
as part of the process to upgrade to Ember 2.3 (from 1.7).
Now, I have a route called recordTypes, which lists all recordTypes. in the legacy code, each recordType was then associated with an itemController 'recordType'. Like so:
{{#each result in searchResults itemController="recordType"}}
...
{{/each}}
Surprisingly, this legacy syntax for Ember did not render anything to the page, but the following one did:
{{#each searchResults itemController="recordType" as |result| }}
...
{{/each}}
The itemController recordType is a legacy Object Controller and the recordTypes controller itself is a legacy Array Controller.
Now, for each result I have a few actions that can be performed. For example, on clicking the result, the editResultName action was to be fired. This action, in the legacy code, was in the recordType controller. Therefore, clicking the item in the recordTypes page would then defer this action to the recordType controller, which would then happily handle the rest.
This is not being fired in ember2.3, even with the legacy controllers. What surprises me more is that this code can be found in ember-legacy-controller.js
export default {
name: 'ember-legacy-controllers',
initialize: function() {
/**
Adds support for ArrayController in the legacy {{each}} helper
*/
Ember._LegacyEachView.reopen({
_arrayController: computed(function() {
var itemController = this.getAttr('itemController');
var controller = get(this, 'container').lookupFactory('controller:array').create({
_isVirtual: true,
parentController: get(this, 'controller'),
itemController: itemController,
target: get(this, 'controller'),
_eachView: this,
content: this.getAttr('content')
});
return controller;
}),
_willUpdate(attrs) {
let itemController = this.getAttrFor(attrs, 'itemController');
if (itemController) {
let arrayController = get(this, '_arrayController');
set(arrayController, 'content', this.getAttrFor(attrs, 'content'));
}
}
});
}
};
Here, it does have a line that references the itemController. However, when this list of searchResults is rendered, and a result is clicked, the error I get is this:
Nothing handled the action 'editResultName'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.
The action is there for sure, but nothing in the itemController is being recognised. Unfortunately a lot of the legacy code I am updating has itemController loops and therefore it would be immensely helpful to be able to use itemController for the time being.
How can I use itemController like it used to be implemented?
Replacing an itemController.
Create a component from the contents inside the each helper. The itemController would become the js side of the component and the template code the template
From this:
{{#each result in searchResults itemController="recordType"}}
<span>result: {{result.title}}</span>
{{/each}}
To this:
{{#each searchResults as |result| }}
{{result-list-item result=result}}
{{/each}}

Programmatically setting computed property of an itemController

I have a template with the following code:
{{#each types itemController='type'}}
<div class='col checkbox'>
<label>
{{input type='checkbox' checked=isSelected disabled=notAllowed}}
<span {{bind-attr class='isSelected'}}>{{name}}</span>
</label>
</div>
{{/each}}
types is set in setupController:
this.store.find('type').then(function(types){
controller.set('types', types);
});`
//Having 2 other models here that I am setting and having an itemController for, exactly in the same fashion as types.
for the ArrayController which has the itemController.
NOTE: To clarify, I am using and setting 3 different models, which work pretty much in the same way as type, that makes this a bit more complicated.
Then the itemController itself:
App.TagController = Ember.ObjectController.extend({
isSelected: function(key, value){
//bunch of code that does some stuff and returns true or false depending on value
}.property()
});
App.TypeController = App.TagController.extend();
Now the problem: I have a resetbutton that should deselect all checkboxes and remove the span classes.
I would have thought about using an action (in the ArrayController) that sets all the isSelected properties to false, but I don't seem to be able to find a way to access and manually set that itemController computed property.
One thing I tried in the ArrayController is the following:
actions: {
resetFilters: function(){
this.get('types').forEach(function(type) {
console.log(type.get('isSelected'));
//type.set('isSelected', false);
});
}
}
But unfortunately this returns undefined. And using jQuery manually to remove the class and uncheck the checkbox seems to work the first instance, but the problem is, the computed property doesn't get updated and that messes things up.
Any idea how I can achieve what I want?
If anything is unclear let me know and I will do my best to clarify.
Thank you.
You are setting controller.types, this will not work with itemController. You should always be setting an array controller's content property.
The following should work:
controller.set('content', this.store.find('type'));
Then to set the isSelected:
controller.setEach('isSelected', false);
This assumes that controller is an instance of an ArrayController that has an itemController set in it's definition, e.g.
App.TypesController = Em.ArrayController.extend({itemController: 'type'});
store.find returns a PromiseArray, so it should be resolved first. You can set the types as follows in setupController:
this.store.find('type').then(function(types){
controller.set('types', types);
});
Or you can resolve types in the reset:
this.get('types').then(function(types) {
types.forEach(function(type) {
console.log(type.get('isSelected'));
});
});
I would recommend the first one though.

ember: How can I set bind-attr for a link-to?

I want to set the style of a link-to helper but don't quite understand how.
I have the following model:
App.ArtistFavorite = DS.Model.extend
name: DS.attr 'string'
image_url: DS.attr 'string'
My template:
li
link-to 'artistFavorite' this {bind-attr style="background-image: url('image-url');"}
But the bind-attr doesn't seem to work
BTW: I'm using emblemjs and coffeescript
link-to is an Ember view helper, so (inspired by this) I was originally going to suggest using attributeBindings, except that raises the following JS error:
Setting 'attributeBindings' via Handlebars is not allowed. Please subclass Ember.View and set it there instead.
It looks like if you really need to set attributes in this way, it is possible to do so by reopening the Ember.LinkView class and setting attributeBindings there, but be forewarned that this will affect every link-to on your page.
But if (as it appears) the only attribute you need to set is style, you can just create a CSS class with your desired style and then set classNames, as discussed here, i.e.:
{{#link-to 'artistFavorite' this classNames="your-class-name"}}
From a code-style perspective, I would go with this approach even if it was possible to (more easily) set the style attribute directly.
Edit: Just realized you are trying to individually set styles for each link using one of the attributes of the corresponding item, so obviously CSS classes would not work. I've thought about this a bit more though.
Although discouraged, you should be able to bind to the style attribute by reopening the LinkView class and adding style to the attributeBindings:
Ember.LinkView.reopen({
attributeBindings: ["style"]
})
Then possibly you could set a value for the style attribute:
{{#link-to 'artistFavorite' this style=favStyle}}
Where favStyle is a computed property on your model or (ideally) your controller:
favStyle: function() {
return "background-image: url('" + this.get('image_url') + "');";
}.property('image_url')
However I haven't tested this and I'm not 100% certain the binding will work correctly this way, because these bindings are typically used for plain-text, not properties.

How can I bind the element ID for an Ember View?

My model "content.id" contains a string, e,g "123":
{{view Em.TextArea idBinding="content.id"}}
Instead of just setting the id of this view to "123", I'd like it to be "message-123", basically customizing the string being used. Sadly, Ember does not allow bindings to be functions, which would solve my problem (I could define such a function on the controller).
What's the best way to achieve this?
You could define a computed property in the controller (or elsewhere):
The controller
MyApp.ApplicationController = Ember.Controller.extend({
content: "a-content",
editedContent: function() {
return "message-" + this.get('content');
}.property('content')
});
The view
MyApp.FooView = Ember.View.extend({
    tagName: 'p'
});
The template (where content is a String, here)
{{#view MyApp.FooView elementIdBinding="editedContent"}}
{{content}}
{{/view}}
And the JSFiddle is here.
EDIT
How can the view see the property editedContent since it belongs on the ApplicationController controller?
The router, after started, automatically render the ApplicationView, or its template when there is no ApplicationView defined. If you want more detail, I suggest you to read the Ember guide: Understanding the Ember.js Router: A Primer.
And {{editedContent}} directly get the controller editedContent property, because the default view context is its controller, as you can read in Ember Blog - 1.0 Prerelease:
The {{#view}} helper no longer changes the context, instead maintaining the parent context by default. Alternatively, we will use the controller property if provided. You may also choose to directly override the context property. The order is as follows:
Specified controller
Supplied context (usually by Handlebars)
parentView's context (for a child of a ContainerView)

How to access computed properties

I have a Ember.Object like:
App.HelpVideoInfo = Ember.Object.extend({
MyAccount: ['FAe8w0cUCJ0', 'How-to: Change "My Account"'],
StoreInformation: ['UddOjEG-hXw', 'How-to: Change Store Information'],
getSecondAccount:function()
{
return this.get('MyAccount')[1];
} .property('MyAccount'),
});
I want to binding from my Ember.View to getSecondAccount (computed property). I used:
App.IconMeaningDialogView = Ember.View.extend({
accountBinding: 'App.HelpVideoInfo.getSecondAccount';
});
But it doesn't work. Thanks.
Your naming conventions are not conform with those from Ember, see Emberist blog post.
You should name classes UpperCase, instances and properties lowerCase. Then the binding works as expected, see http://jsfiddle.net/pangratz666/PQYYH/.
I would also recommend to use the built in array accessor objectAt.
Handlebars:
<script type="text/x-handlebars" >
{{#view App.IconMeaningDialogView}}
{{account}}
{{/view}}
</script>​
JavaScript:
App.helpVideoInfo = Ember.Object.create({
myAccount: ['FAe8w0cUCJ0', 'How-to: Change "My Account"'],
storeInformation: ['UddOjEG-hXw', 'How-to: Change Store Information'],
secondAccount: function() {
return this.get('myAccount').objectAt(1);
}.property('myAccount').cacheable()
});
App.IconMeaningDialogView = Ember.View.extend({
accountBinding: 'App.helpVideoInfo.secondAccount'
});​
The way to access that property would be as follows
App.IconMeaningDialogView = Ember.View.extend({
accountBinding: Ember.Binding.from('App.HelpVideoInfo.getSecondAccount')
});
Refer to Advanced Emberjs Bidnings
You probably don't want to extend an Ember.Object to create your class App.HelpVideoInfo. Instead, if you create a new object there, it would allow you to bind to the properties in the way you want.
However, if you would like to use the App.HelpVideoInfo subclass, then you would have to create an object before you can bind to its properties. An example of how you can do it this: http://jsfiddle.net/74KX7/1/