Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get type backbone model?

I am learning backbone now. And I have a problem like this: can I get type backbone model. Backbone have a function like typeof in javascript or instanceof in java. Like that:

getModelTypeot: function(model) {
    // return model type 
} 
like image 260
Ulug'bek Avatar asked Mar 07 '13 12:03

Ulug'bek


1 Answers

In JavaScript each object has a reference to its constructor (a function that was used to create the object). It's accessible as obj.constructor.

If you have a Backbone.js model, which is extended from Backbone.Model in this way: var YourModel = Backbone.Model.extend({});, you could create an object using new: var yourModel = new YourModel();.

Then, you could use yourModel.constructor:

yourModel.constructor === YourModel // true

Or instanceof:

yourModel instanceof YourModel // true
yourModel instanceof Backbone.Model // true
like image 51
eur00t Avatar answered Oct 02 '22 16:10

eur00t