1

I have a backbonejs model called User. User has properties city, state, and country. I have a function on this model called locationString that outputs a comma separated list based on if properties on the model are presnet or not. i.e. Atlanta, Georgia or Georgia or Atlanta so there are only commas if there need to be.

I am setting the json for my template in the render method of my view by calling var json = model.toJSON() and then setting location string by doing json.location_string = this.model.locationString();.

I also am binding the change event of the model to my render method by calling this.model.bind('change', this.render); in my initialize function on the view.

All other attributes changed on the model update in the view appropriately but the location_string property does not.

Any help will be greatly appreciated

location_string: function() {                                                                                                                                                                                      var loc_str = "";      
loc_str += this.city || "";      
loc_str += (this.city && this.geo_state) ? ", " : "";      
loc_str += this.geo_state || "";      
loc_str += (this.geo_state && this.country) ? ", " : "";      
loc_str += this.country || "";      
return loc_str;    
},
kwbock
  • 647
  • 5
  • 13
  • Are you sure `json.location_string = this.model.locationString();` is producing a different value the second time your `render` gets called? – Paul Hoenecke Apr 11 '13 at 19:57
  • Good question, I just checked that and it is returning the same thing. I have the locationString function in a _.bindAll on the user model. Is there something I should be differently here? – kwbock Apr 11 '13 at 20:04
  • Hmm, well if `locationString()` returns the same thing every time, you should post that function because the problem is likely there. – Paul Hoenecke Apr 11 '13 at 20:37
  • i added the location string function to the post. – kwbock Apr 11 '13 at 21:16

1 Answers1

1

I figured out it was because i was calling this.field_name as opposed to this.get('field_name').

I hope this helps other people.

kwbock
  • 647
  • 5
  • 13