Given an instance of Backbone model, how can I know the "class" (so to speak) of this instance ?
For instance:
class Car extends Backbone.Model mycar = new Car()
And what I need is:
mycar.modelName # => 'Car'
Model. Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control. You extend Backbone.
js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
I tried Mitch's suggestion but in Chrome, model.constructor.name is always ""
for me.
I chose instead to use a convention. I now create a class property using the second param of extend that works as follows:
directory.models.SomeModel = Backbone.Model.extend({ // usual model instance stuff }, { modelType: "SomeModel" // class property });
I found that I can always get what I want like this:
var mt = model.constructor.modelType;
As of writing you can get a model's class name by doing the following:
model.constructor.name
This works for me in the Chrome browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With