Defining Iterative Block Helpers in Meteor 0.8

Prior to version 0.8, you could use the usual Handlebars method to define iterative auxiliary blocks, such as popular each_with_key, defined, for example , as follows:

Handlebars.registerHelper("each_with_key", function(obj, fn) {
    var context,
        buffer = "",
        key,
        keyName = fn.hash.key;

    for (key in obj) {
        if (obj.hasOwnProperty(key)) {
            context = obj[key];

            if (keyName) {
                context[keyName] = key;
            }

            buffer += fn(context);
        }
    }

    return buffer;
});

This no longer works at 0.8, and neither the migration guide nor the spatial records documentation provide an example of this.

Given that block helpers are now treated as inclusions, and inclusions need to return a template (or zero) instead of HTML, I don’t know how much this is possible at the moment.

+1
source share
1 answer

, . 0,8, . , , :

JS:

UI.registerHelper('addKeys', function (all) {
    return _.map(all, function(i, k) {
        return {key: k, value: i};
    });
});

HTML:

{{#each addKeys obj}}
<div>
   {{key}}: {{value}}
</div>
{{/each}}
+3

Source: https://habr.com/ru/post/1535474/


All Articles