简体   繁体   中英

ember.js show store data in template

I have JSON data I am retrieving from a server and I want to show it in a template in ember.js. I have data saved in the store and I want to render the data in the template.

My template

<script type="text/x-handlebars" data-template-name="DieDaten" >
<p>{{name}} some text2</p>

 <ul id="todo-list">
 {{#each person in model}}
        <li>
          <label>{{person.firstName}} and text</label>
        </li>
  {{/each}}
  </ul>

 <button id="clear-completed" {{action 'click'}}>
    Submit
  </button>

</script>

and my associated controller

App.DieDatenController = Ember.Controller.extend({ 

   model: function () {
     return this.store.all('person');
   }

});

When the page loads, I get this.

Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed function () {
    return this.store.all('person');
  }

How do I fix this so it works?

Is there a simpler/better way to render the JSON in the template besides saving to the store, then rendering the store?

model is not supported in Ember.Controller it is rather a part of Ember.Route . So, you're code should be:

App.DieDatenRoute = Ember.Route.extend({ 

  model: function () {
    return this.store.all('person');
  }

});

You can read more about specifying a route's model at http://emberjs.com/guides/routing/specifying-a-routes-model/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM