I must be missing something simple because I'm not finding the answer to my question anywhere. I've done due RTFM diligence, and am now resorting to asking my question here.
In short, I want to put a simple <a> tag in an Ember template, but the extra Ember mark up to make the object dynamic is breaking the URL itself.
My template looks like this:
<script type="text/x-handlebars" data-template-name="event-nav">
{{ obj.display_name }}
</script>
This, of course, results in something like this sitting in the DOM itself:
<a class="event-name logo" href="/<script id='metamorph-0-start' type='text/x-placeholder'></script><script id='metamorph-0-end' type='text/x-placeholder'></script>"><script id="metamorph-1-start" type="text/x-placeholder"></script>Object Name<script id="metamorph-1-end" type="text/x-placeholder"></script></a>
So in short, how do I make it do?
Much thanks.
You would use the 'bindAttr'-helper for this kind of use case:
<script type="text/x-handlebars" data-template-name="event-nav">
<a {{bindAttr href="obj.url" alt="obj.displayName"}}>{{obj.displayName}}</a>
</script>
Related
I need to pick up an ember.js project to finish a few things. Unfortunately I don't have time to learn the whole framework and features but I was wondering how I create a partial.
I read the documentation below but don't really understand what goes where. I presume the second block of text goes in the .hbs template but where do I put the top block so that the second block can be rendered on any .hbs template?
<script type="text/x-handlebars" data-template-name='_author'>
Written by {{author.firstName}} {{author.lastName}}
</script>
<script type="text/x-handlebars" data-template-name='post'>
<h1>{{title}}</h1>
<div>{{body}}</div>
{{partial "author"}}
</script>
I worked it out, I didn't add the partial to:
EmberHandlebarsLoader.loadTemplates([])
I'm running into a problem. I need App.rootElement = "body", because views and components will be used all over the place. I do not want to put the entire page in a handlebars template, due to SEO and other concerns. The main application template will exist somewhere in the center of the page:
<h1>Header markup</h1>
<script type="text/x-handlebars">
<h2>Application Template</h2>
{{outlet}}
</script>
<h1>Footer markup</g1>
However, when the page is rendered, this template is appended to the end of the body, instead of staying where the template script has been placed. (see jsbin exmaple) Is there any way to tell Ember to render the application template where it is in the markup?
One hacky solution I've found is to manually move the containing element in didInsertElement, but that seems a little heavy handed and not something that would be considered a best practice. (see jsbin example)
The script tag doesn't have anything to do with placement, if you want it to live somewhere create a div tag, id it, and change the rootElement to that div tag.
<script type="text/x-handlebars">
<h2>Application Template</h2>
{{outlet}}
</script>
...
<h1>Header markup</h1>
<div id='foo'></div>
<h1>Footer markup</h1>
App.rootElement = '#foo';
I have been trying to wrap my head around how Ember routing works on a very basic level and need a little kick in the right direction. I am providing the code I have written so far, hopefully you can follow along and see what is going on
I would like to see a page hierarchy like so:
index
--Red
----Light
----Medium
----Dark
--Green
----Light
----Medium
----Dark
--Blue
----Light
----Medium
----Dark
The "medium" colors are actually going to be the index for each nested route, So I think I need router.js set up like so:
Colors.Router.map(function(){
this.resource('red', { path: '/red' }, function(){
this.route('light-red');
this.route('dark-red');
});
this.resource('green', { path: '/green'}, function(){
this.route('light-green');
this.route('dark-green');
});
this.resource('blue', { path: '/blue'}, function(){
this.route('light-blue');
this.route('dark-blue');
});
});
For now, I just want to serve up static html templates (with just a little bit of #link-to handlebars for navigation)... just so I can see a working prototype.
The main template is here
<div id="emberColors">
<header id="mainHeader">
<h1>Ember Colors</h1>
<p>Pick a color</p>
<nav class="navi main">
<ul>
<li>{{#link-to "red" activeClass="active"}}Red{{/link-to}}</li>
<li>{{#link-to "blue" activeClass="active"}}Blue{{/link-to}}</li>
<li>{{#link-to "green" activeClass="active"}}Green{{/link-to}}</li>
</ul>
</nav>
</header>
{{outlet}}
<footer id="footer">
<p>Ok Buh-bye</p>
</footer>
I have a template for each of the three colors, each is set up like this:
<section id="blue">
<header>
<h1>The Blue</h1>
<p>Blue is cool.</p>
<nav class="navi sub">
<ul>
<li>{{#link-to "blue.light-blue"}}Light{{/link-to}}</li>
<li>{{#link-to "blue"}}Base{{/link-to}}</li>
<li>{{#link-to "blue.dark-blue}}Dark{{/link-to}}</li>
</ul>
</nav>
</header>
{{outlet}}
</section>
And I broke up each "shade" into multiple templates
<div class="color-lt">
<p>Light Blue</p>
</div>
Note: I also understand that I can reuse templates and load different model data into them, rather than hard coding lots of little templates... but I am just trying to focus on learning the routing at the moment.
Because these are just basic templates, I assume (based on what I have read) Ember auto-generates a lot of the MVC and I don't need to define any specific controllers, models, or routes (except for perhaps the IndexRoutes for the three main color landing pages).
I understand that the naming convention for the templates will help ember do most of the work.
However it is the naming conventions that are confusing me.
in router.js it seems simple enough to define the routes by how I want it to appear in the URL
in handlebars {{#link-to}} though, it looks like I need to reference the URL (or template?) name with a . separation (include dashes or no)?
and when defining an index route it is structured with PascalCase?
I need some help with figuring out how to properly name/declare these routes so that Ember can render them correctly.
for instance: because of the nested nature of these pages... do I need to store my templates in a nested directory file structure as well? or is that being done artificially by ember routing?
Is there a general MVC Routing methodology that I am supposed to understand before I can understand Ember Routing specifically?
Sorry, I have a lot of questions about this... It seems like it is supposed to be really simple, but I don't know why I am not getting it. Any help will be greatly appreciated.
EDIT: I will also let you know that, as I have it written out currently, Ember Inspector is not throwing any errors that I can see. So I am convinced, that so far I am on the right track, but I am just not providing anything for the router to resolve...
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.
I am missing something very fundamental to being able to render templates in emberjs. I found a few examples online..
github.com/elucid/ember-tunes
github.com/emberjs/examples
I'm still not able to get my stuff working. These are also using a previous version of emberjs.
ember-yii.sk.hj.cx/site/app seemed promising as well but, I was still not able to get my stuff working.
Does anyone know of examples specific to v1.0.0-pre.4 ?
I'm trying to render a header, body, and footer as separate outlets. Here is the body of app.html:
<div id="main">
<script type="text/x-handlebars" data-template-name="app">
<header>
{{outlet header}}
</header>
<section>
{{outlet body}}
</section>
<footer>
{{outlet footer}}
</footer>
</script>
</div>
What I'm not understanding is how to render templates within the 3 {{outlet}} placeholders.
I watched various EmberJS "tutorials", read the documentation. This is why I believe I'm missing something very fundamental.
Any help, guidance, or direction to helpful examples will be very appreciated.
#akshayrawat has provided an absolute gold mine at: https://gist.github.com/akshayrawat/4701510 This is literally a list of 1.0.0-pre4 EmberJS resources.
#trek, one of the core EmberJS contributors created his own version of the Todos app at: https://github.com/trek/ember-todos-with-build-tools-tests-and-other-modern-conveniences
I found this particularly useful when I was trying to figure out how to use multiple {{outlet}} placeholders.
Ps: The peepcode screencast on EmberJS costs 12 USD.. but it is really good and based on Embe 1.0.0-pre4