Reputation: 7092
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;
}
Upvotes: 0
Views: 39
Reputation: 458
u.k is correct. If you declare that variable in the initialize then that's going to be a one time thing since the initialize is the constructor function. So, by splitting that out into its own separate function you will be able to determine whether that statement is true or false in real time as a user is using the application.
I would definitely keep that split out as a function if it were me. However, if that same function is used across multiple views then it might make sense to create a utility class and then require/import that utility in each of the views that use it.
Upvotes: 1
Reputation: 3091
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.
Upvotes: 1