json data not rendered in backbone view

Posted by user2535706 on Stack Overflow See other posts from Stack Overflow or by user2535706
Published on 2013-06-30T03:33:07Z Indexed on 2013/06/30 4:21 UTC
Read the original article Hit count: 176

Filed under:

I have been trying to render the json data to the view by calling the rest api and the code is as follows:

var Profile = Backbone.Model.extend({       
    dataType:'jsonp',
    defaults: {
        intuitId: null,
        email: null,
        type: null      
    },  
});     
var ProfileList = Backbone.Collection.extend({      
    model: Profile,         
    url: '/v1/entities/6414256167329108895'
});     
var ProfileView = Backbone.View.extend({        
    el: "#profiles",
    template: _.template($('#profileTemplate').html()),         
    render: function() {
        _.each(this.model.models, function(profile) {
            var profileTemplate = this.template(this.model.toJSON());
            $(this.el).append(tprofileTemplate);
        }, this);
        return this;        
    }
});     
var profiles = new ProfileList();   
var profilesView = new ProfileView({model: profiles});  
profiles.fetch();
profilesView.render();

and the html file is as follows:

<!DOCTYPE html> 
<html>
    <head> 
        <title>SPA Example</title> 
        <!-- 
            <link rel="stylesheet" type="text/css" href="src/css/reset.css" />
            <link rel="stylesheet" type="text/css" href="src/css/harmony_compiled.css" /> 
        --> 
    </head>
    <body class="harmony">
        <header>        
            <div class="title">SPA Example</div>    
        </header>
        <div id="profiles"></div>   
        <script id="profileTemplate" type="text/template"> 
            <div class="profile"> 
                <div class="info"> 
                    <div class="intuitId"> 
                        <%= intuitId %> 
                    </div> 
                    <div class="email"> 
                        <%= email %> 
                    </div> 
                    <div class="type"> 
                        <%= type %> 
                    </div> 
                </div> 
            </div> 
        </script>
    </body>
</html>

This gives me an error and the render function isn't invoking properly and the render function is called even before the REST API returns the JSON response.

Could anyone please help me to figure out where I went wrong. Any help is highly appreciated Thank you

© Stack Overflow or respective owner

Related posts about backbone.js