1

I have the following backbone.js script. Traditionally I was rendering the view by using:

var myItem = new Item();

myItem.fetch(); var reward_view = new ListView({model:myItem}); reward_view.render();

However, most of the examples I have seen don't do this and somehow allow you to load/init the view by using using:

var listView = new ListView();

How? This is what I have tried:

(function($){

   var Item = Backbone.Model.extend({
        urlRoot: '/api/v1/1/'
    });


  var List = Backbone.Collection.extend({
    model: Item
  });


  var ListView = Backbone.View.extend({

    el: '#reward_view',

       initialize: function(){
      _.bindAll(this, 'render'); // every function that uses 'this' as the current object should be in here

      this.collection = new List();
      this.render();

    },

     render: function(){
         console.log(this)
      $(this.el).html('<h1>Hello' + this.model.get('name') + '</h1>');
      return this; // for chainable calls, like .render().el
    }


  });
1
  • 1
    You need to bind the model or collection. You are almost there tho. Nov 7, 2013 at 10:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.