Search code examples
javascriptbackbone.js

What's the convention to define backbone models class properties/fields


I want to define which properties a class has. We use backbone for OOP. I've read that backbone model gets its properties when initialized, and only methods are defined for the class using extend. But I think that having explicitly defined class fields adds to readability. Is there some convention on how to do that?


Solution

  • From the fine manual:

    extend Backbone.Model.extend(properties, [classProperties])
    [...] as well as optional classProperties to be attached directly to the constructor function.

    Similarly for collections, routers, and views.

    To define a class method on a model:

    var M = Backbone.Model.extend({
        // instances methods and properties go here...
    }, {
        some_class_method: function() { ... }
    });
    
    M.some_class_method(); // Then this will work.