0

Can someone explain to me what the 'this' in "this.model.toJSON()" refers to? I'm reasoning that the 'this' should refer to the object ContactView, since that is the object that its located in. But that doesn't seem to be the case. Is 'this.model' connected to the variable Contact?

var Contact = Backbone.Model.extend({
    defaults: {
    photo: "/img/placeholder.png"
  }
});

var ContactView = Backbone.View.extend({
   tagName: "article",
   className: "contact-container",
   template: $("#contactTemplate").html(),

   render: function () {
      var tmpl = _.template(this.template);

    this.$el.html(tmpl(this.model.toJSON()));
    return this;
   }
});
Roman C
  • 49,761
  • 33
  • 66
  • 176
pontus222
  • 79
  • 4
  • 1
    possible duplicate of [JavaScript "this" keyword](http://stackoverflow.com/questions/3127429/javascript-this-keyword) – Tom Panning Jun 11 '14 at 17:21
  • I'm flagging this question because it is a specific case of "what does 'this' mean in JavaScript?", which is a good question, but there is already a canonical answer on SO. – Tom Panning Jun 11 '14 at 17:22
  • Why isn't it the view? The view has a model, the contact. – Dave Newton Jun 11 '14 at 17:24
  • @TomPanning I don't buy the general-purpose answer in the case of a specific framework/etc since `this` binding in JS depends a lot on the context of the JS, how this function is called, etc. – Dave Newton Jun 11 '14 at 17:27
  • Okay. So a view automatically receives a model when it's created? You don't have to pass it as a parameter to the view? – pontus222 Jun 11 '14 at 17:41
  • I'm with @DaveNewton here, that question is relevant but the specifics of how `this`, events, a view's `model` option, ... behave in Backbone are probably at the root of the problem so I'm not pulling out the Golden Close Hammer. – mu is too short Jun 11 '14 at 17:43
  • How do you instantiate your `ContactView`? If you want a `model` property, you have to say `new ContactView({ model: something })`, Backbone won't figure it out on its own. – mu is too short Jun 11 '14 at 17:44
  • 1
    @DaveNewton If this isn't a general question on "this", then it should be edited to be a specific question about what "this" means in Backbone views – Tom Panning Jun 11 '14 at 17:46
  • @TomPanning I'm trying to clarify what the problem really is. Anyone that is familiar with Backbone would recognize this as a Backbone problem more than a JavaScript problem. – mu is too short Jun 11 '14 at 17:49
  • @TomPanning The question already states they're asking about `this` in a specific context. – Dave Newton Jun 11 '14 at 17:52
  • @DaveNewton Do you agree that a better form of this question would be "What does 'this' refer to in a Backbone View"? Then it's not a general 'this' question, and also not a "what's going on in my code". – Tom Panning Jun 11 '14 at 17:54

1 Answers1

0
 contact = new Contact({name:Mike,location:'Chicago'});
 contactView = new ContactView({model: contact});
 contactView.render();

Yes, you're right. 'this' have the current context, in your case it is the current view object. If you're executing your view using above code snippet, this.model.toJSON() return the json object.

sajeevp
  • 42
  • 3