Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the model type for a passed in backbone.js model instance

How do I retrieve the model name for a backbone.js model instance?

For example:

var Topic = Backbone.Model.extend({

})
var topic = new Topic({ type: 'question' })

var doSomethingWithTopic = function(topic) {
  // check if passed in topic is of type Topic
  // something like topic.constructor.name === 'Topic'
}

doSomethingWithTopic(topic)

I realize I may be blurring the line between a backbone.js model and a class, so I am open to other ways of going about this if needed.

like image 913
baalexander Avatar asked Aug 17 '11 03:08

baalexander


People also ask

How can we get the attribute value of a model in Backbone JS?

js Get model is used to get the value of an attribute on a model. Syntax: model. get(attribute)

Is Backbone JS still used?

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.

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.


1 Answers

Use the instanceof operator.

var doSomethingWithTopic = function(topic) {
  if( topic instanceof Topic ) {
    // do something with topic
  }
}
like image 73
Lee Avatar answered Oct 24 '22 16:10

Lee