Search code examples
backbone.jsbackbone-views

helper functions inside a view


I have been given the task of maintaining a webapp that uses backbone. The previous maintainer left and is unavailable to answer any questions.

I've fixed almost all the problems and issues I've found, but there is one that is really leaving me scratching my head.

In one view, there are a a bunch of helper functions like this:

isVerifiedByAdmin: function() {
    return this.model.get('verificationDate') !== null;
},

My question is, wouldn't it just be easier to put this in the initialize method of the view, like this?

initialize: function (attrs) {
    this.isVerifiedByAdmin = this.model.get('verificationDate') !== null;
}

Solution

  • The value of "verificationDate " in the model in the initialize state might not be the same as the value when calling isVerifiedByAdmin().

    It's hard to tell without more information, but seems like a different use case. It could be that in your application it wouldn't make any difference.