Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6020781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:37:05+00:00 2026-05-23T03:37:05+00:00

I have a simple question about Backbone.js’ get and set functions. 1) With the

  • 0

I have a simple question about Backbone.js’ get and set functions.

1) With the code below, how can I ‘get’ or ‘set’ obj1.myAttribute1 directly?

Another question:

2) In the Model, aside from the defaults object, where can/should I declare my model’s other attributes, such that they can be accessed via Backbone’s get and set methods?

var MyModel = Backbone.Model.extend({
    defaults: {
        obj1 : {
            "myAttribute1" : false,
            "myAttribute2" : true,
        }
    }
})

var MyView = Backbone.View.extend({
    myFunc: function(){
        console.log(this.model.get("obj1"));
        //returns the obj1 object
        //but how do I get obj1.myAttribute1 directly so that it returns false?
    }
});

I know I can do:

this.model.get("obj1").myAttribute1;

but is that good practice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:37:06+00:00Added an answer on May 23, 2026 at 3:37 am

    While this.model.get("obj1").myAttribute1 is fine, it’s a bit problematic because then you might be tempted to do the same type of thing for set, i.e.

    this.model.get("obj1").myAttribute1 = true;
    

    But if you do this, you won’t get the benefits of Backbone models for myAttribute1, like change events or validation.

    A better solution would be to never nest POJSOs (“plain old JavaScript objects”) in your models, and instead nest custom model classes. So it would look something like this:

    var Obj = Backbone.Model.extend({
        defaults: {
            myAttribute1: false,
            myAttribute2: true
        }
    });
    
    var MyModel = Backbone.Model.extend({
        initialize: function () {
            this.set("obj1", new Obj());
        }
    });
    

    Then the accessing code would be

    var x = this.model.get("obj1").get("myAttribute1");
    

    but more importantly the setting code would be

    this.model.get("obj1").set({ myAttribute1: true });
    

    which will fire appropriate change events and the like. Working example here: http://jsfiddle.net/g3U7j/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question about rails syntax: How can I find out to
Trying to get started with subsonic but have a simple question about stored procedures
I have maybe very very Simple Question: Where i can find documentation about expressions
I have simple question about .net garbage collection. In the following code I create
a simple question about maven repository server can a pom file have dependencies of
I have simple question about MVC routing. How i can construct Html.ActionLink thhat generates
I just have one simple question about XSS attack. I know that you can
I have a simple question about Java's XML API and I hope there's a
I have a language-agnostic question about an algorithm. This comes from a (probably simple)
I have a simple question about debugging on Xcode and GDB. I often run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.