I'm fairly new to JavaScript, and entirely new to Backbone.js.
In the code below, I am creating a new UserLocationModel and passing this model instance to a new UserLocationView object. In this view's initialize()
I (try to) bound render()
to any changes happening to the passed along model.
I'm fairly certain the model changes, I receive new latitude and longitude values from navigator.geolocation
and I assign these values to the model's values. I also know that the view succesfully receives the model (this.model is never null).
Why is UserLocationView.render() not being called?
Model:
var UserLocationModel = Backbone.Model.extend({
defaults: {
latitude: -1,
longitude: -1
},
updateLocation: function() {
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(this.positionSuccess, this.positionFailure);
}
},
positionSuccess: function(position) {
this.set({
latitude : position.coords.latitude,
longitude : position.coords.longitude
});
},
positionFailure: function() {
...
}
});
View:
var UserLocationView = Backbone.View.extend({
initialize: function(){
_.bindAll(this,'render');
if(this.model) this.model.on('change',this.render,this);
},
render: function(){
console.log('UserLocationView render called.');
...
}
});
Execution start:
window.MyApp = Backbone.View.extend({
initialize: function() {
userLocationModel = new UserLocationModel();
userLocationView = new UserLocationView({ model: userLocationModel });
userLocationModel.updateLocation();
this.render();
}
});
Edit:
I have rewritten some of the code above, to include model.set
.
Edit 2: Additional information for moxn.
main.js:
define(['jquery', 'underscore', 'backbone','template', 'myapp'], function() {
$(document).ready(function() {
window.App = new MyApp({ appendTo: $('body') });
});
});
called by this in the HTML:
<script data-main="../static/js/main" src="../static/js/vendor/require.js"></script>