I’m looking for some examples for creating a custom constructor on my models. I want the structure the model/data differently then just setting it as attributes.
Can somebody show me some basic example of how to do this?
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you really want to override the constructor, pass a
constructorproperty toBackbone.Model.extend(), e.g.:If you want to call the built-in constructor from your custom constructor, you can do something like:
Or if you don’t want to have to repeat the name of the variable containing the parent class all over the sub class, or you don’t want to worry about the value of that variable changing, you can do something like the following:
Or if you prefer the way @Claude suggests, but repeating the sub class variable name within the sub class instead of the parent class var name:
If you want more advice than that, you’ll have to be more specific about what you want to accomplish.
Anything that you just want to do after the built-in constructor functionality, you should probably do in
initialize().