Specifying all properties in Backbone.Model

I am using Backbone in a mobile project. Say I have a pattern class similar to this.

var Person = Backbone.extend({ }); 

The Person class contains the following properties firstName , lastName , age and gender . I want to specify all these properties in a class. Thus, other developers know what properties they should set for the instance. After looking at the documentation that I see, there is a property called defaults that I could use.

 var Person = Backbone.extend({ defaults: { firstName: '', lastName: '', age: null, gender: null } }); 

But I see that the purpose of the defaults property is another right? Do not let people know what properties the class contains. Is there a better way to achieve this?

+5
source share
2 answers

In our project, we use defaults for this purpose. This solves the problem well enough and can also serve as a point of documentation. There is no other way to what I know. However, you can still use old-fashioned comments :)

+1
source

You can write getter functions, so model attributes are available as an open model API.

Thus, any developer using the model object will be able to view its public properties (provided that it uses a decent IDE), without even opening the file for editing (why?), For example:

 var Person = Backbone.extend({ defaults: { firstName: '', // ... }, getFirstName: function () { return this.attributes.firstName; } // ... }); 
+1
source

Source: https://habr.com/ru/post/1205358/


All Articles