How to implement nested every loop using special helpers (handlebar)

I am trying to create a table with Ember.js and Handlebar.js. Unfortunately, I'm stuck on how to create cells that consist of Ember.TextArea with a value of Binding.

The idea is as follows. There is a domain model project that has many records (from 1 to n). Both domain models are different objects and are not implemented (records are not embedded in the project).

Each project has an array called keys, which contains several lines (for example, "a", "b", "c", ...). In addition, each entry contains an array of elements. Each of these elements also has one of the above keys as a property key. See Detailed Model below.

Models

Project

{ keys: ['a', 'b', 'c'] } 

Record

 { parameters: [ { key: 'a', value: 'aaaa' }, { key: 'b', value: null }, { key: 'c', value: '123' } ] } 

The goal is to ultimately create an HTML table containing a column for each project key (in the above case, there should be 3 columns with the headers "a", "b", "c"). For each record that is now associated with the project (in other words, loaded in the background in the ArrayController), there should be a line. And now the tricky part comes: each cell must be bound to the corresponding element of the "parameters" array (the cell content must be Ember.TextArea).

 || a || b || c || ----------------------------------------------------------------------------- | entry 1 binding to element of parameters array where key = 'a' | ... | ... | ---------------------------------------------------------------------------- | entry 2 binding to element of parameters array where key = 'a' | ... | ... | ----------------------------------------------------------------------------- 

Approach i

Building table headers is obviously pretty easy and, of course, no problem:

 <table> <thead> <tr> {{#each key in project.keys}} <th>{{key}}</th> {{/each}} </tr> </thead> <tbody> ... <tbody> </table> 

The part where I do not know how to create the code is the body. Iterating over the elements in the array controller is possible yes, but I have no idea how to bind it to the corresponding element. The difficulty is that the binding is set to the correct element. For example, in the column for the key "a", the binding should be associated with the element of the parameter array, where element.key === 'a'.

 <tbody> {{#each entry in App.entriesController}} <tr> {{#each key in project.keys}} ??? {{/each}} </tr> {{/each}} </tbody> 

I was thinking about using the descriptor helper, but found out that Ember.js (which is different from the handlebar.js documentation) does not pass the object itself, it only passes the name of the property, which can then be used to get the value. Unfortunately for the array, I don’t see how to get the record of the array that is currently being processed in each loop.

The idea was to pass the record and key to the helper, and then return an instance of Ember.TextArea, which is bound to the correct element in the entry.parameters array.

Note. Iterating over the project.keys files, we can guarantee that the order of the keys will be the same as in the header (the same iteration order).

Any other ideas on how to approach this problem?

+4
source share
2 answers

The idea of ​​a crazy bath: you can use Ember.Object as a proxy, dynamically defining linearized properties on it (in Coffeescript for sanity, names are from braindump only):

  TheProxy = Ember.Object.extend fill: (projects, entries) -> for project in projects entry = # find entry for project for key,value of entry this.set "value_for_#{project.id}_#{key}", value 

After that, you can generate the template in the same way by creating the bindings {{value_for_xxx_yyyy}} and associating with it a completed instance of TheProxy.

+1
source

The best solution here is to write a specialized helper helm. Unfortunately, it’s not so easy to make your custom related helpers now. There is an old PR here that we would like to update and get for 1.0 https://github.com/emberjs/ember.js/pull/615 . You might get some ideas from this to accomplish your own implementation.

+4
source

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


All Articles