Using Ember's Handlebars #each helper within a block helper that slices an array into chunks

frekw

I'm trying to slice an ArrayController's content into chunks (for presentational purposes). Within each chunk I want to iterate over the items in order to render everything properly.

I've tried accomplishing this by writing a partition Handlebars block helper, which seems to work. However, when I try to loop over the content of each chunk, each throws the following error: "Assertion failed: The value that #each loops over must be an Array. You passed [object Object]"

helpers.js

Ember.Handlebars.registerHelper('partition', function(path, options){
  path = path === '' ? 'this' : path;
  var arr = [];
  var data = Ember.Handlebars.get(this, path, options).content;
  var ret = '';
  while(data.length){
    arr.push(data.splice(0, options.hash.size || 2));
  }
  for(var i = 0, len = arr.length; i < len; i++){
    ret = ret + options.fn(arr[i]);
  };
  return ret;
});

template.hbs

<h2>Your photos</h2>
{{#partition controller.photos size=2}}
  <div class="slide">
    {{#each this}}
      {{this.title}}
    {{/each}}
  </div>
  <br/><br/><br/>
{{/partition}}

this within the #partition is the correctly sized array.

How can I get this to work? Where is each getting its content from?

gist can be found here: https://gist.github.com/frekw/2d3627039b42891a0b0a

Scott

I solved your issue, see this JSBin for a demo.

  • In the helper I create a new Ember.Object containing the partitioned collection. This object is passed as the context to options.fn().

  • I added a with helper within the block to set the context for the each block.

{{#partition photos size=2}}
  <div class="slide">
    {{#with collection}}
      {{#each}}
        {{title}}
      {{/each}}
    {{/with}}
  </div>
  <br/><br/><br/>
{{/partition}}

The helper:

Ember.Handlebars.registerBoundHelper("partition", function(collection, options){

  var size = options.hash.size || 2;

  while(collection.length > 0)
  {
    var oc = Ember.Object.create({
      collection: collection.splice(0, size)
    });
    options.fn(oc);
  }

});

I hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Refering to 'this' from handlebars each helper within handlebars-helper inArray block helper

From Dev

Handlebars Block helper similar to #each

From Dev

Ember/Handlebars: Localization Helper with parameter array

From Dev

compare block helper for handlebars

From Dev

Handlebars- block helper inside block helper

From Dev

jQuery doesn't work in a custom ember's handlebars helper

From Dev

What's the difference between Block helper and Partials in Handlebars.js

From Dev

Handlebars sometimes not print {{this}} in each helper

From Dev

Handlebars sometimes not print {{this}} in each helper

From Dev

Using Ember, how do I dynamically bind a property in a view helper contained in an each block?

From Dev

Ember: Access controller property from within an each helper

From Dev

Handlebars is saying it's missing the helper?

From Dev

Block helper with ember-cli

From Dev

Example of using Handlebars lookup helper

From Dev

How to define a custom helper in handlebars ember

From Dev

Ember js use handlebars helper inside a controller?

From Dev

Pass argument to event handler in Ember handlebars helper

From Dev

How to pass (date) object to Ember handlebars helper?

From Dev

How to pass (date) object to Ember handlebars helper?

From Dev

How to define a custom helper in handlebars ember

From Dev

Pass argument to event handler in Ember handlebars helper

From Dev

Ember js display each item in javascript array in {{#each}} helper

From Dev

How do I get access to the block contents of a custom Ember handlebars helper?

From Dev

How do you create a Handlebars block helper in Ember that can wrap/manipulate content?

From Dev

Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

From Dev

Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

From Dev

ember-cli support for Handlebars @vars in each helper (i.e., @index, @key, @first, @last)

From Dev

How to render a simple list in ember.js handlebars template with each helper?

From Dev

Ember / Handlebars: Get Application controller in a handlebars helper function

Related Related

  1. 1

    Refering to 'this' from handlebars each helper within handlebars-helper inArray block helper

  2. 2

    Handlebars Block helper similar to #each

  3. 3

    Ember/Handlebars: Localization Helper with parameter array

  4. 4

    compare block helper for handlebars

  5. 5

    Handlebars- block helper inside block helper

  6. 6

    jQuery doesn't work in a custom ember's handlebars helper

  7. 7

    What's the difference between Block helper and Partials in Handlebars.js

  8. 8

    Handlebars sometimes not print {{this}} in each helper

  9. 9

    Handlebars sometimes not print {{this}} in each helper

  10. 10

    Using Ember, how do I dynamically bind a property in a view helper contained in an each block?

  11. 11

    Ember: Access controller property from within an each helper

  12. 12

    Handlebars is saying it's missing the helper?

  13. 13

    Block helper with ember-cli

  14. 14

    Example of using Handlebars lookup helper

  15. 15

    How to define a custom helper in handlebars ember

  16. 16

    Ember js use handlebars helper inside a controller?

  17. 17

    Pass argument to event handler in Ember handlebars helper

  18. 18

    How to pass (date) object to Ember handlebars helper?

  19. 19

    How to pass (date) object to Ember handlebars helper?

  20. 20

    How to define a custom helper in handlebars ember

  21. 21

    Pass argument to event handler in Ember handlebars helper

  22. 22

    Ember js display each item in javascript array in {{#each}} helper

  23. 23

    How do I get access to the block contents of a custom Ember handlebars helper?

  24. 24

    How do you create a Handlebars block helper in Ember that can wrap/manipulate content?

  25. 25

    Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

  26. 26

    Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

  27. 27

    ember-cli support for Handlebars @vars in each helper (i.e., @index, @key, @first, @last)

  28. 28

    How to render a simple list in ember.js handlebars template with each helper?

  29. 29

    Ember / Handlebars: Get Application controller in a handlebars helper function

HotTag

Archive