jsetting32
jsetting32

Reputation: 1632

Call action function when template is loaded (Ember.js)

I currently have an issue with my application where I would like to load a bunch of data when a page is being rendered

Like stackoverflow.com, when a client clicks on the profile link on the nav bar, a new page is rendered with a bunch of data being fetched from an API.

How, in ember, would this be done? I am making a GET request to an API i implemented and I would like to display all the data from the response. But how would I call such a function in the situation I'm in. Heres the code:

index.html :

<script type="text/x-handlebars">
<div id="header">
  <div id="wrapper">
    <a id="logo" href="/socialWOD">&nbsp</a>
    <ul id="nav">
      <li>{{#link-to "about" activeClass="selected"}}About{{/link-to}}</li>
      <li>{{#link-to "wods" activeClass="selected"}}WODs{{/link-to}}</li>
      <li>{{#link-to "login" activeClass="selected"}}Login{{/link-to}}</li>
      <li id="search">
        <form>
          <input type="text" id="st-search-input" class="st-search-input" autocomplete="off"
                  autocorrect="off" autocapitalize="off" style="outline: none;" placeholder="Search"/>
        </form>
      </li>
    </ul>
  </div>
</div>
{{outlet}}
</script>


<!-- WODs List Template (Start)-->
<script type="text/x-handlebars" data-template-name="wods">
<div id="socialWODapp">
  <div id="newentry">
    <h1>socialWOD</h1>
    {{input type="text" id="new-WOD-wod-category" placeholder="Enter new WOD category name" value=category action="create"}}
    {{input type="text" id="new-WOD-name" placeholder="Enter WOD name" value=name action="create"}}
    {{input type="text" id="new-WOD-description" placeholder="Enter WOD description" value=description action="create"}}
    {{input type="text" id="new-WOD-workout" placeholder="Enter WOD workout" value=workout action="create"}}
  </div>

  <div id="main">
    <ul id="wodlist">
      {{#each}}
      <h1>{{wod_category}}</h1>
      <li>
        <input type="checkbox" class="toggle">
        <label>{{name}}</label><button class="destroy"></button>
      </li>
      <li>{{description}}</li>
      <li>{{workouts}}</li>
      {{/each}}
    </ul>
    <input type="checkbox" id="toggle-all">
  </div>
</div>
</script>
<!-- WODs List Template (End)-->

wod_controller.js:

SocialWOD.WodsController = Ember.ArrayController.extend({
        actions: {
            getWODs: function() {
                Ember.$.post('/socialWOD_API/?wods', data).then(function(response) {
                        console.log(response.message);
                    });
            }
        }
}

NOTE: WODs Stands for Workout of the Day

How do I go about calling getWODs() when the WODs page is rendered? I am able to use static WOD FIXTURES but when I implement the GET request, of course, nothing is being called or fetched. Anyone have ideas?

EDIT: Sweet... I should have known that ... So simple... Heres what I did:

SocialWOD.WodsRoute = Ember.Route.extend({
        model: function() {
            Ember.$.get('/socialWOD_API/?wods').then(function(response) {
                    return response;
                });
        }
    });

Now I have an issue about rendering the information... In the console, I get this:

[{"objectId":"086f8db206","wod_category":"The New Girls","name":"Annie","description":"50-40-30-20-10 rep rounds, for time","workouts":"[\"Double-unders\",\"Sit-ups\"]","createdAt":"2014-07-31 13:24:33","modifiedAt":"2014-07-31 13:24:33"},{"objectId":"389b7518f4","wod_category":"The New Girls","name":"Eva","description":"5 rounds for time","workouts":"[\"Run 800 meters\",\"30 x 2-pood KB swings\",\"30 x Pull-ups\"]","createdAt":"2014-07-31 13:24:33","modifiedAt":"2014-07-31 13:24:33"},{"objectId":"7d91a57aea","wod_category":"The New Girls","name":"Kelly","description":"5 rounds for time","workouts":"[\"Run 400 meters\",\"30 x Box jump 24\"\",\"30 x Wall-ball\"]","createdAt":"2014-07-31 13:24:33","modifiedAt":"2014-07-31 13:24:33"},{"objectId":"07b6fa146c","wod_category":"The New Girls","name":"Lynne","description":"5 rounds for time","workouts":"[\"Max rep BW Bench\",\"Max rep BW Pull-ups\"]","createdAt":"2014-07-31 13:24:33","modifiedAt":"2014-07-31 13:24:33"},{"objectId":"0e834bd262","wod_category":"The New Girls","name":"Nicole","description":"As many rounds as possible in 20 minutes","workouts":"[\"Run 400 meters\",\"Max rep Pull-ups\"]","createdAt":"2014-07-31 13:24:33","modifiedAt":"2014-07-31 13:24:33"}] 

WHy isn't my template rendering the information? Sorry, I am just very new to Ember and using web framworks... plain php seems so much simpler :P

Upvotes: 2

Views: 1534

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

You need to actually return the get call to the route ;)

SocialWOD.WodsRoute = Ember.Route.extend({
        model: function() {
          return Ember.$.get('/socialWOD_API/?wods').then(function(response) {
                    return response;
                });
        }
});

http://emberjs.jsbin.com/dukiq/1/edit

Upvotes: 2

Related Questions