Emberjs adding `.property()` in a function attribute of a controller/route - ember.js

I'm trying to do the same as this question
My real question is why did he have to add .property() at the end of the function in the errorClass attribute? And can someone please point me to a documentation on how this work?
when I don't include the .property(), a stringed function of the attribute is placed as value in my template.
Handlebar:
<div class="tab-pane fade show {{loginActiveClass}}" id="login" role="tabpanel">
Expectation:
<a data-toggle="tab" href="#login" role="tab" class="nav-link active">
What happens when I discard the .property():
<a data-toggle="tab" href="#login" role="tab" class="nav-link function loginActiveClass() {
return this.get('page') === 'login' ? 'active' : '';
}">

With {{att}} in your hbs you access the value of the component's or controller's attribute/property named att.
By your description, your attribute loginActiveClass has a value of type function. This is what you get in your handlebars. Since the position where you use it only admits string values, you get the toString() representation of your value, which for a function is the function's code as string.
You cannot trigger actual evaluation of the function code by simply accessing it with {{att}} in your .hbs.
Adding .property() at the end gives you a computed property that does indeed evaluate the function and returns the evaluation result to the "caller". (Note that the preferred syntax for computed properties in Ember has changed a while ago to something more explicit. I recommend using eslint and eslint-plugin-ember to let your IDE or ember's cli inform you about best practices when appropriate.)
Also, in your specific example, the "logic" of the function is easily integrated into the hbs:
<div class="tab-pane fade show {{if (eq page 'login') 'active'}}" id="login" role="tabpanel">

Related

How to mutate an array's value in "each" helper in Ember JS (Octane)

I have an array of strings passed as an argument to a component, inside the component I am using "each" helper to render each string in a text input. I tried the following approach.
MainComponent.hbs
<Component #model={{model}}/>
//Eg: model.list = ["Jack", "Sparrow"];
Component.hbs
<div>
{{#each this.args.model.list as |name|}}
<div>
<PaperInput #value={{ name }}
#placeholder="Enter a Name"
#onChange={{action (mut name)}}/>
</div>
{{/each}}
</div>
I am running into the error "Uncaught (in promise) Error: Assertion Failed: You can only pass a path to mut". Would really appreciate if anyone can let me know What's going wrong here.
The value that are derived from helpers (each in your case) cannot be mutated using mut helper as the helpers usually don't pass down or hold the values to change the original property.
For instance,
It makes sense if we are mutating a value as below where capitalize is a helper:
<button {{action (mut name) (capitalize name)}}>
Capitalize
</button>
however, below snippet does't fit right as helper returns the value one way!
<button {{action (mut (capitalize name)) name}}>
Capitalize
</button>
the same thing is going on with the each helper and the looped through value cannot be mutated! This code comment might be useful for further digging.
You can change your snippet to handle the onChange in the backing component class instead:
<div>
{{#each this.args.model.list as |name index|}}
<div>
<PaperInput #value={{ name }}
#placeholder="Enter a Name"
#onChange={{this.changeName index}}/>
</div>
{{/each}}
</div>
// component.js
changeName(index, nameToBeUpdated) {
// change the name here...
}
Figured it out. Posting the full implementation for the benefit of others. I passed down the index value to component's action as suggested in Gokul's answer but ran into another problem. There was no straight forward method to change the array's value. So I used the Mutable Array's replace method do that. That again caused another problem, every time I entered a character in the text input it was chaning the array value and re rendering the list which took the focus out of the input. So in "each" helper I had to set key="#index" which tells the helper to rerender only if there is a array index change, not the value.
Component.js
#action
updateName( index, name ) {
this.args.model.list.replace(index, 1, [name]);
}
MainComponent.hbs
<Component #model={{model}}/>
Component.hbs
{{#each this.args.model.list key="#index" as |name index|}}
<div>
<PaperInput #value={{ name }}
#placeholder="Enter a Name"
#onChange={{action this.updateName index}}/>
</div>
{{/each}}

create x number of components within a component in ember 2.8

So I have two components in my Ember app, game-instance and game-card. The idea is that I will pass a 'cards' number attribute to game-instance and it will create x instances of game-card. Here's the setup
game.hbs (the main file):
<div>
{{input type="number" min="2" max="24" placeholder="enter a number from 4 to 24"
value=inputValue
key-up=handleInputChange}}
{{game-instance cards=inputValue}}
</div>
game-instance.hbs
<div>
<!-- iteratively create game cards based on cards attribute passed -->
</div>
game-card.hbs
<div class="gamecard">
<img src="" />
</div>
When trying to figure out how to write up the game-instance.hbs logic, I've seen how handlebars allows you to iterate over an array. This is not really what I want. What I want is this behaviour:
game-instance reads its cards attribute passed from game.hbs. In this case let's assume 6
a type of for-loop logic creates six instances of game-card within game-instance.
Is there an appropriate handlebars syntax to achieve this? If not, how can I achieve similar behaviour? Thanks very much.
Idea is you need to use component helper along with #each block with cards array. so that whenever we change cards array,it will re-render the whole block.
After user entering input,you can create/update cardsArray. that will automatically rerender game-card component in game-instance component
game-instance.hbs
{{#each cardsArray as |value,index|}}
{{component 'game-card' value }}
{{/each}}
Whenever we change cardsArray using pushObject or chagning the entire reference then then each block will be rendered.

How to use a component from within a component in Ember

G'day all,
I'm trying to wrap a component with another to provide a simplified editing wrapper.
The component is to conditionally show either a label or a select component that allows the user to pick the right value.
I want to wrap the power-select component, and pass it's values through to the sub-component, so the page template component reference looks like this:
{{cm-select
title="Country ##"
options=countries
selected=selectedCountry
searchField="name"
action="selectCountry"
}}
"countries" is an array of country objects, and selectedCountry is one of those country objects.
The component template has the following:
<td>{{title}}</td>
<td>
{{#if edit}}
{{#power-select
options=options
selected=selected
searchField=searchField
as |object|}}
{{object.name}}
{{/power-select}}
{{else}}
<small>{{modelElement}}</small>
{{/if}}
</td>
Unfortunately the power-select component renders with an empty list of options.
I thought wrapping those parameters in handlebars might do the trick, but it seems that handlebars in handlebars isn't a valid syntax.
Does anyone have any ideas?
Thanks,
Andy
That should work, I created a twiddle for you, demonstrating your use case. You'll see I updated the your cm-select template to this:
{{title}} |
<button {{action 'toggleEdit'}}>Toggle Edit</button>
<br/>
{{#if edit}}
Search for a Item via {{searchField}}
<br/>
{{power-select
options=options
selected=selected
searchField=searchField
onSelect=(action "itemSelected")
}}
{{else}}
{{search-list
options=options
searchField=searchField
onSelect=(action "itemSelected")
}}
{{/if}}
Where you iterated over options for power-select in the cm-select component, I moved that down into the power-select template. It's better to try and encapsulate some functionality there, instead of having everything in cm-select. I wasn't sure what you had in mind with {{modelElement}} so I just demonstrate what it would look like, using two different components in cm-select.

Ember.js: Route-dependent Menu

I've got a Template which defines an <div id="sidemenu"><ul>...</ul></div>.It should has some <li>-Elements which represents general available-Menu-Items. On Sub-routes I want to add some additional menu-entries which only make sense on that routes. But since the div where to put the li's into is defined in the parent-route-template, I don't know how I should implement this.
My first theoretical approach was to call a controller function which returns the necessary li's. I could override this function in each sub-controller but I don't know if this is good practice.
However I want to do it in a ember-vanilla way if possible (someone told me to use a plugin called "wormhole" or something)
I would make the dependent portions of the menu a named outlet, then render into the outlet from each route's renderTemplate hook.
{{! components/sidebar/template.hbs }}
<div id="sidemenu">
<ul>
<li>Generally available menu item 1</li>
{{outlet name='nav'}}
^^^^^^^^^^^^^^^^^^^^^
</ul>
</div>
// thing1/route.js
renderTemplate() {
this.render('thing1-nav-template', {into: 'nav'});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this._super(...arguments);
}
You could use {{ember-wormhole}} I suppose--I don't know that module. It certainly does not fall into the category of "ember vanilla" if that's one of your criteria. Essentially, it would allow you to write the thing1-nav-template contents directly in your route template instead of another template. I have no idea how stable it is, so personally I'd do it the old-fashioned way using outlets; that's what they're there for.
You can use https://github.com/yapplabs/ember-wormhole to render content from one template into an HTML element identified by an ID.
Menu Template:
<ul>
<li>General stuff</li>
</ul>
<ul id='foobar' />
Subroute Template:
{{#ember-wormhole to="foobar"}}
<li>Subroute stuff</li>
{{/ember-wormhole}}

Ember.Component (block form): more than one outlet {{yield}}

I see that ember has a very nice mechanism for wrapping content in a component using the {{yield}} mechanism documented here.
So, to use the example in the documentation, I can have a blog-post component template defined like so:
<script type="text/x-handlebars" id="components/blog-post">
<h1>{{title}}</h1>
<div class="body">{{yield}}</div>
</script>
I can then embed blog-post into any other template using the form:
{{#blog-post title=title}}
<p class="author">by {{author}}</p>
{{body}}
{{/blog-post}}
My question is, can I specify two different {{yield}} outlets in the components template?
Something like this is possible via Named Outlets in Ember.Route#renderTemplate like so:
Handlebars:
<div class="toolbar">{{outlet toolbar}}</div>
<div class="sidebar">{{outlet sidebar}}</div>
JavaScript:
App.PostsRoute = Ember.Route.extend({
renderTemplate: function() {
this.render({ outlet: 'sidebar' });
}
});
I'm not sure I can take this path for a component which will not know what route's template would be rendering it.
EDIT 1:
For the sake of clarity, I'm trying to implement the Android Swipe for Action Pattern as an Ember component.
So, I'd like users of this component to be able to specify two different templates:
A template for the normal list item, and
A template for the actions that are revealed when a swipe on (1) is detected.
I want to make this into a component, because quite a lot of javascript goes into handling the touch(start/move/end) events, while still managing smooth touch based scrolling of the list. Users would supply the two templates and this component would manage handling of touch events and necessary animations.
I've managed to get the component working in the block form, where the block's contents are treated like (1). The second template (2) is specified through a parameter (actionPartial below) which is the name of a partial template for the actions:
Component Handlebars Template: sfa-item.handlebars
<div {{bind-attr class=":sfa-item-actions shouldRevealActions:show" }}>
{{partial actionPartial}}
</div>
<div {{bind-attr class=":sfa-item-details isDragging:dragging shouldRevealActions:moveout"}}>
{{yield}}
</div>
Calling Handlebars Template:
{{#each response in controller}}
<div class="list-group-item sf-mr-item">
{{#sfa-item actionPartial="mr-item-action"}}
<h5>{{response.name}}</h5>
{{/sfa-item}}
</div>
{{/each}}
Where the mr-item-action handlebars is defined like so:
mr-item-action.handlebars:
<div class="sf-mr-item-action">
<button class="btn btn-lg btn-primary" {{action 'sfaClickedAction'}}>Edit</button>
<button class="btn btn-lg btn-primary">Delete</button>
</div>
Problem is, actions from the user supplied partial, sfaClickedAction above, are not bubbled up from the component. A fact which is mentioned in the docs in para 4.
So, now I do not know how a user could capture actions that he defined in the supplied actions template. A component cannot catch those actions because it doesn't know about them either.
EDIT 2
I sprung a follow up question here
This blog post describes the most elegant solution for Ember 1.10+: https://coderwall.com/p/qkk2zq/components-with-structured-markup-in-ember-js-v1-10
In your component you pass yield names into {{yield}}s:
<header>
{{yield "header"}}
</header>
<div class="body">
{{yield "body"}}
</div>
<footer>
{{yield "footer"}}
</footer>
When you invoke your component, you accept the yield name as a block param... and use an esleif chain!
{{#my-comp as |section|}}
{{#if (eq section "header")}}
My header
{{else if (eq section "body")}}
My body
{{else if (eq section "footer")}}
My footer
{{/if}}
{{/my-comp}}
PS eq is a subexpression helper from the must-have ember-truth-helpers addon.
PPS Relevant RFC: proposal, discussion.
Since it is not possible to have two {{yield}} helpers within one component (how would the component know where one {{yield}}'s markup stops and the next one begins?) you may be able to approach this problem from a different direction.
Consider the pattern of nested components. Browsers do this already with great success. Take, for example, the <ul> and <li> components. A <ul> wants to take many bits of markup and render each one like a member of a list. In order to accomplish this, it forces you to separate your itemized markup into <li> tags. There are many other examples of this. <table>, <tbody>, <tr>, <td> is another good case.
I think you may have stumbled upon a case where you can implement this pattern. For example:
{{#sfa-item}}
{{#first-thing}}
... some markup
{{/first-thing}}
{{#second-thing}}
... some other markup
{{/second-thing}}
{{/sfa-item}}
Obviously first-thing and second-thing are terrible names for your specialized components that represent the things you'd want to wrap with your first and second templates. You get the idea.
Do be careful since the nested components won't have access to properties within the outer component. You'll have to bind values with both outer and inner components if they are needed in both.