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

  • Home
  • SEARCH
  • 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 7919603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:55:21+00:00 2026-06-03T15:55:21+00:00

I need to fetch data having a specific id and which id is defined

  • 0

I need to fetch data having a specific id
and which id is defined in the view instance.
Here the example, see the comments in MyModel definition:

// my view instance
var myView = new MyView({
    model: {id: 12321}
});

MyView = Backbone.View.extends({
        initialize: function()
        { 
            myModel.fetch();
        }
});

MyModel = Backbone.Model.extends({
        url: function url ()  
        {
          // how to get the id passed to view instance?
          return "http:..../id/" + this.id; 
        }
});
  • 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-06-03T15:55:23+00:00Added an answer on June 3, 2026 at 3:55 pm

    Model should not has any knowledge of the existence of the View, so the View should be the one that sais to the Model which id to fetch:

    MyView = Backbone.View.extends({
            initialize: function()
            { 
                myModel.id = this.model.id;
                myModel.fetch();
            }
    });
    

    (I’ve used your example code as template for my example, but I have to say I feel several weird things on it, I suppose is just a matter of taste)

    Update: My very personal taste opinions

    Is very difficult to do this but as you requested I’ll share with you my very personal code review of your example code. Take this as it is: a very humble opinion.

    this.model confused

    I would not use attribute names that can create confussion:

    var myView = new MyView({
        model: {id: 12321}
    });
    

    Into this instance this.model is making reference to a raw Hash but in a Backbone context this is against the intuitive feeling that this is gonna be a Backbone.Model.

    I rather change it for something like this:

    var MyView = Backbone.View.extend({
      initialize: function( opts ){
         this.model_id = opts.model_id;
      }
    })
    var myView = new MyView({ model_id: 12321 });
    

    I think this naming is more intuitive.

    close variables scopes

    This code can only works if myModel is in an scope bigger that it should be:

    MyView = Backbone.View.extends({
            initialize: function()
            { 
                myModel.fetch();
            }
    });
    

    I rather prefer using more encapsulated scopes, even if myModel has been declared in the out-side context of your View the View should use a variable of its private context. For example

    var MyView = Backbone.View.extends({
      initialize: function( opts ) { 
        this.model = opts.model;
        this.model.fetch();
      }
    });
    
    var myView = new MyView({ model: myModel });
    

    Check the detail that I have also added var in front of MyView because if not MyView will be a window global variable.

    use the Backbone urlRoot

    In your example, this …

    MyModel = Backbone.Model.extends({
        url: function url ()  
        {
          // how to get the id passed to view instance?
          return "http:..../id/" + this.id; 
        }
    });
    

    … can be summarized as this:

    MyModel = Backbone.Model.extends({
        urlRoot: "http:..../id"
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I need to fetch: - posts that have comments - number of
Im using vb.net and I need to fetch data from two different tables and
What do I do, I need to fetch data from 3 tables in mysql,
I have Core Data setup in my app and need to fetch a bunch
I need to fetch all email id's of contacts which is present in contact
I am creating a moderating application in which i need to fetch all the
i need to fetch a url with javascript/jquery and not php. i've read that
I need to fetch only first record (because I need last date) of resultset,
I often need to fetch tgz files, decompress them, and then delete the tgz.
I've got a HashMap and I need to fetch an item by its integer

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.