0

I am fetching the JSON using the library philsturgeon/codeigniter-restserver When I check in the response section of console, I get the proper JSON values returned like

this: [{name:test,address:test},{name:test2,address:test2}].

On fronted I want these values to be displayed in the template. But it just returns {"readyState":1} when I do <%= JSON.stringify(ledgers) %>.

I call the template from my view like this:

$("#container-left").html(this.template({ledgers:app.ledgers.fetch()}));

My REST controller function looks like:

public function index_get()
{

    $this->response($this->db->get('ledgers')->result());
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
beNerd
  • 3,314
  • 6
  • 54
  • 92

1 Answers1

1

Your aren't using the fetch method correctly. Because fetch doesn't return the data when it is called so you need to provide the success callback to fill your template:

var self = this;
app.ledgers.fetch({
    success: function(model, response, options) {
        $("#container-left").html(self.template({ledgers: model}));
    }
});

The _.each function itself won't return anything so you when you output the return value you get undefinied.

You need to output the data inside your function with <%= name %>

<% _.each(ledgers, function(data){  %> <%= name %> <% }) %>

Demo JSFiddle.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • ok that throws me a nasty error which i think is solely because of not knowing the ledgers object. Is there a way i can see the structure of the ledger object as i am fetching it from some remote source? – beNerd Jan 23 '13 at 08:49
  • When you generating the template you can specify a variable name where you can access the whole object what is passed in the template as the data. Then you can output the current context in your template: see http://jsfiddle.net/yh6kp/. Or just debug your fetching code to see how is the returned data look like. – nemesv Jan 23 '13 at 08:58
  • I only get this {"readyState":1} when i do a JSON.stringify(ledgers). But when i check response headers, i am getting the full correct json data. – beNerd Jan 23 '13 at 09:31
  • Ok then your `ledgers` is not a collection so you don't need the each: to write out the value you need `<%= ledgers.readyState %>` which will outputs 1. – nemesv Jan 23 '13 at 09:32
  • I need to fetch json that i receive from the backend and display in frontend. Backend returns the right data. Here is what i receive from backend: [{name:test,address:test},{name:test2,address:test2}]. I want to iterate over this JSON. – beNerd Jan 23 '13 at 09:37
  • @MrinalPurohit You are now far away from you original question. You should extend (or better create a new) question with your current problem: how is your template looks like, how do you generate the template, and how is the data looks like which you use for the template. – nemesv Jan 23 '13 at 09:40
  • Yes now i got it. Just wanted to know that as we have used the self variable for scope issues, is there a better way to do this? Or this approach is fine? Thanks a ton for your help though. – beNerd Jan 23 '13 at 10:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23208/discussion-between-mrinal-purohit-and-nemesv) – beNerd Jan 23 '13 at 10:22