I need to be able extract a backbone model's name at runtime from a backbone View to write generic View and then Template code. That is: I need to get the "ClassName" from the model that is passed to the View and then take the first three characters from it and pass it on to the template.
Is there a simple way to do it?
Thanks.
Bharat
By ClassName
, do you mean the name by which you refer to your model, such as User
in new User();
?
If so, unfortunately this is not possible, because the name you give to the model class is simply a variable name and not part of the model declaration. Consider:
var Foo = Backbone.Model.extend({});
var Bar = Foo;
Is the ClassName
Foo
or Bar
?
Instead, simply give each of your models a name
property:
var Foo = Backbone.Model.extend({
name:"Foo"
});
This way you can easily get the model name, either by modelInstance.name
or ModelClass.prototype.name
.