Friday, 15 March 2013

javascript - Backbone - cannot read property 'get' of undefined -



javascript - Backbone - cannot read property 'get' of undefined -

this first time trying create backbone application server endpoint , have found myself in bit of snag.

i have underscore template in html here:

<tbody> <% _.each(users, function(user) { %> <tr> <td><% user.get('firstname') %></td> <td><% user.get('lastname') %></td> <td><% user.get('age') %></td> <td></td> </tr> <% }); %> </tbody>

and here model/view/collection:

var users = backbone.collection.extend({ url: '/uma.service/service1.svc/', model: user });

var user = backbone.model.extend({ defaults: { "firstname": "", "lastname": "", "age": null } }); var userlist = backbone.view.extend({ el: '.page', render: function() { console.log(this.collection.tojson()); var template = _.template($('#user-list-template').html(), {users: this.collection.model}); this.$el.html(template); }, initialize: function() { var = this; this.collection = new users(); this.collection.fetch({ success: function(user) { that.render(); } }); } });

i not sure messing up. thought might have how calling in collection or model have looked while , cannot find if there is. able help me out?

i have console log in render function in view , returns of info cannot see why not working. have ideas?

instead of:

_.each(users, function(user) {...

try doing:

users.each(function(user) { ...

backbone "proxys" various underscore methods onto backbone objects (in case collection object). more info check out: http://backbonejs.org/#collection-underscore-methods , scroll downwards little few of examples.

edit:

@bojangels right. don't want pass this.collection.model template. want pass collection through (in case). collection "contains" models care about. collection.model attribute on collection says type of model collection contains, not specific instance of model itself.

as @tallmaris stated in comment bellow, missing piece of puzzle utilize <%= putput values:

<td><%= user.get('firstname') %></td>

javascript backbone.js underscore.js

No comments:

Post a Comment