1

is it possible to access Backbone.js model's attribute or function in javascript? If possible how to do that??

My model :

var TimeZone = Backbone.Model.extend({
    defaults:{
        timeZoneID : 'EST5EDT',
        timeZoneShortForm : "ET",
        timeZoneLongForm : "Eastern Time",
     }
   });

Javascript

 getTimeZoneId :function(TimeZone)          
    {
           //here for given TimeZone model.. i have to acess the timeZoneID
    }

Now i have to access timeZoneID.. How it is possible?

3 Answers 3

2

I presume you are trying to access an instance of TimeZone:

var timeZone = new TimeZone();
timeZone.get('timeZoneID');

http://documentcloud.github.com/backbone/#Model-get

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for ur reply.. yes, the mistake is i was not passing the instance.. Thanks :)
@Stupid karma is a good reward for people who work to help you, I recommend you "upvote" and/or "accept" the answers those have helped you.
1

Note that Backbone.js is a JavaScript framework, so your Backbone model is JavaScript, just like your getTimeZoneId function.

Backbone.Model.extend() creates a model constructor function, with which you’re expected to create instances (e.g. var mymodel = new TimeZone();).

It’s not quite clear from your question whether you want to get the timeZoneID of a model instance, or the timeZoneID property from the defaults object of your TimeZone prototype model object.

If you’ve got an TimeZone instance called mymodel, you can get its timeZoneID like this:

 mymodel.get('timeZoneID');

However, you can’t (I don’t think) inspect the defaults object of TimeZone. Backbone.Model.extend() returns a function which doesn’t expose any of the properties of the object you pass to it.

Comments

0

Like ponzano said, it seems you want the following:

getTimeZoneId :function(timeZone){
  return timeZone.get('timeZoneID');
}

Note however, that the timeZone function argument is lower case. Don't use the upper case, or you will mix it up with the model "class".

Of course, sine Backbone already define getters and setters, you might not need your extra function at all...

In addition, maybe you shouldn't have the var in front of the model definition so that it is part of the global namespace and accessible form everywhere within your application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.