samedi 25 avril 2015

Is it possible to include custom args in an Ember partial?

There's a piece of code in my template that's repeated multiple times through it... So I want to DRY it. It's a simple piece of code with no logic on it.

First, I though about creating an array with the title and values for a loop, but then I remembered Handlebars does not allow me to do it.

Second, I though on partials. They're only a part of a template, afterall. But then I noticed it apparently ignores additional arguments. Thus, there's no way to alter it's contents besides what's already in my model. In this and in the prior case, I didn't want to mess my route/controller with template-only variables.

Third, my current solution is a Component, but it seems so dirty. However, it's quite troublesome to have a JS file with only an empty object; furthermore, components have no file structure, and I wanted this piece of code to live along with it's original template.

This is a simplification of what I wanted to achieve:

project/index.hbs

{{partial "counter_block" value=model.count_success title="Yeah!"}}
{{partial "counter_block" value=model.count_failures title="Boo :("}}

project/_counter_block.hbs

<div>
    <h2>{{title}}</h2>
    <span>{{value}}</span>
</div>

Is there any solution for this matter or is this just another case where Handlebars oversimplification causes coding issues?




Aucun commentaire:

Enregistrer un commentaire