Search code examples
javascriptember.jsember-dataember-clijsbin

Each model Ember not listing the models in template


I am using Ember.RSVP.hash to create different models in the same route, i successfully create the model records in the store, i can see the data in the console. enter image description here

The problem is that i can only list one the two models in my template. ( repos name but not the commit message ).

Here the code

Route

    var gitrepositoriesPromise = function() {
        return Ember.$.ajax(reposUrl, {
          success: function(repos) {
            return repos.map(function(repo) {
                return store.createRecord('repo', {
                    name: repo.name,
                    description: repo.description
                });
            });
          },
          error: function(reason) {
             reject(reason);
          }});

    };

    var gitactivitiesPromise = function() {
        return Ember.$.ajax(eventsAct, {
            success: function(events) {
                return events.filter(function(event) {
                    return event.type == 'PushEvent';
                }).forEach(function(item){
                    return item.payload.commits.map(function(commit){
                        return store.createRecord('commit', {
                            message: commit.message,
                        });
                    });
                });
            },  
            error: function(reason) {
             reject(reason);
        }});             
    };      



    return Ember.RSVP.hash({
        commits: gitactivitiesPromise(),
        repos: gitrepositoriesPromise()
    });

Template

   <ul>
    {{#each model.repos}}        
      <li>{{name}}</li>
    {{/each}}
   </ul>
   <ul>
    {{#each model.commits}}  
       <li>{{message}}</li>
    {{/each}}
   </ul>

So the problem must be here in

{{#each model.commits}}  
   <li>{{message}}</li>
{{/each}}

What am i doing wrong? here the jsbin reproducing the issue.


Solution

  • Ok, so problem was in your gitactivitesPromise function. I've modified your approach to use Ember.RSVP.hash:

    var gitactivitiesPromise = function() {
      return new Ember.RSVP.Promise(function (resolve) {
        Ember.$.ajax(eventsAct, {
          success: function(events) {
            var result = [];
            events.filter(function(event) {
              return event.type == 'PushEvent';
            }).forEach(function(item){
              item.payload.commits.map(function(commit){
                result.push(store.createRecord('commit', {
                  message: commit.message,
                }));
              });
            });
            resolve(result);
          },  
          error: function(reason) {
            reject(reason);
          }
        });
      });
    };
    

    This lets you to access message this way in template:

    Commits:
    <ul>
      {{#each model.commits}}
        <li>{{this.message}}</li>
      {{/each}}
    </ul>
    

    Result:

    screenshot

    Working demo.