Sunday, 8 September 2013

Adding a value to a prototype object in backbone not working

Adding a value to a prototype object in backbone not working

In backbone I have a view with a custom prototype object called
"mistakes_dict". I want to keep a tally of the mistakes made, so I want
the mistakes_dict to persevere. However I am getting an error when the
"next_stage" method runs that mistakes_dict is not defined. Why is this
happening? When I define the mistakes dict locally in the nexstage method
it works fine but of course then the same dictionary isn't available the
next time the method runs.
As a side note how do I make the dictionary create a new key if it doesn't
already exist (with a value of 1) and increment it by 1 if it does exists?
I know how to do this in Python but I am new to JS.
window.View = Backbone.View.extend({
mistakes_dict: {},
initialize: function () {
this.render();
},
events: {
"click .nextstage" : "nextstage"
},
nextstage: function () {
var mistakes_string = $("span.highlighted").text();
for(var i = 0, len = mistakes_string.length; i < len; i++){
mistakes_dict[mistakes_string[i]] = 1;
};
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});

No comments:

Post a Comment