Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backbone.js how to create static method in model?

Tags:

backbone.js

For Example I have the following code

var myModel = Backbone.Model.extend({
    foo: function(){
       console.log('in foo..')
    }
});

This foo method works if I instantiate myModel but is there any way to access it without instantiating ?

like image 803
Siva Avatar asked Mar 29 '15 16:03

Siva


1 Answers

You can pass it as the second argument to extend:

var myModel = Backbone.Model.extend(
    // instance properties
    {
        foo: function() {
           console.log('in foo..')
        }
    },
    // static
    {
        bar: function() {
           console.log('in bar..')
        }
    }
);

Here, foo will be available only in instances of myModel, and bar can be called statically. myModel.bar().

like image 159
Michael Zajac Avatar answered Oct 21 '22 20:10

Michael Zajac