lundi 1 février 2016

Ember 2.0 How can I make template and route talk to each other?

I am teaching myself Ember-cli and am trying to build a simple application, which fetches results when user searches for a term. I am stuck at the first level, As of now am passing a hardcoded query search term, I am getting the ajax result just fine, but I am not sure how to "pass" that to the template

This is my hbs code /app/templates/product-search.hbs

hello {{!-- just for testing --}}

<ul>
    {{#each item in result.content}} {{!-- even tried looping over result --}}
        <li>Product Info: {{item.id}}</li>
    {{/each}}
</ul>

This is the route file: /app/routes/product-search.js

import Ember from 'ember';

export default Ember.Route.extend({
    model: function(params){
        var term = 'random';
        var fetchURL = 'http://ift.tt/20CkzRm'+term;
        var result = [];
         $.ajax({
          url: fetchURL,
          dataType: 'jsonp',
          success: function(response){
            result.set('content', response.results);
           }
        });
        console.log("Search Results In Model: ");
        console.log(result);
        return result;
    }
});

This is what I am getting in console enter image description here

I am not sure how best to process the result set to be able to display it. Also I don't even know how to start the second piece, where in ideally I would want the search term to be a user input, by a text box, how would I send that back to the route?

Any help is appreciated, I've been stuck with this for a couple hours now.




Aucun commentaire:

Enregistrer un commentaire