Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Ember.js handlebars #each iterate over objects?

I'm trying to make the {{#each}} helper to iterate over an object, like in vanilla handlebars. Unfortunately if I use #each on an object, Ember.js version gives me this error:

Assertion failed: The value that #each loops over must be an Array. You passed [object Object]

I wrote this helper in attempt to remedy this:

Ember.Handlebars.helper('every', function (context, options) {
  var oArray = [];
  for (var k in context) {
    oArray.push({
      key   : k,
      value : context[k]
    })
  }
  return Ember.Handlebars.helpers.each(oArray, options);
});

Now, when I attempt to use {{#every}}, I get the following error:

Assertion failed: registerBoundHelper-generated helpers do not support use with Handlebars blocks.

This seems like a basic feature, and I know I'm probably missing something obvious. Can anyone help?

Edit:

Here's a fiddle: http://jsfiddle.net/CbV8X/

like image 572
lxe Avatar asked Feb 12 '14 19:02

lxe


People also ask

What is Handlebars in Ember JS?

Ember uses the Handlebars templating library to power your app's user interface. Handlebars templates contain static HTML and dynamic content inside Handlebars expressions, which are invoked with double curly braces: {{}} . Dynamic content inside a Handlebars expression is rendered with data-binding.

How do I create a .HBS file?

To create the HBS file, customize your settings, select File → Save Setlist As..., name the file, choose the save location, and click Save. To open the H5S file, double-click the file or select File → Open Setlist..., navigate to the file, and click Open. NOTE: The HBS extension was replaced by .

How do I create a helper in Ember?

To implement the helper, we write a JavaScript function that takes its arguments as an array. This is because helpers can also receive named arguments, which we'll discuss next. import { helper } from '@ember/component/helper'; function substring(args) { let [string, start, end] = args; return string.

Is HBS and HTML same?

HTML is the language that browsers understand for laying out content on a web page. . hbs stands for Handlebars, the name of a tool that lets you write more than just HTML.


2 Answers

Use {{each-in}} helper. You can use it like like {{each}} helper.

Example:

{{#each-in modelWhichIsObject as |key value|}}
  `{{key}}`:`{{value}}`
{{/each-in}}

JS Bin demo.

like image 127
Daniel Kmak Avatar answered Oct 29 '22 22:10

Daniel Kmak


After fiddling with it for a few hours, I came up with this hacky way:

Ember.Handlebars.registerHelper('every', function(context, options) {
  var oArray = [], actualData = this.get(context);

  for (var k in actualData) {
    oArray.push({
      key: k,
      value: actualData[k]
    })
  }

  this.set(context, oArray);
  return Ember.Handlebars.helpers.each.apply(this, 
      Array.prototype.slice.call(arguments));
});

I don't know what repercussions this.set has, but this seems to work!

Here's a fiddle: http://jsfiddle.net/CbV8X/1/

like image 40
lxe Avatar answered Oct 29 '22 21:10

lxe