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 9003353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:42:03+00:00 2026-06-16T00:42:03+00:00

In the context of a Collection, I want to retrive a model instance based

  • 0

In the context of a Collection, I want to retrive a model instance based on some object containing model data, but I don’t want to hard code the idAttribute.

Backbone makes things easy when you already have a model instance, you can just access its .id property and it sorts things out, but I can’t seem to find a way of going the other way, short of creating a instance of a model just to get at its idAttribute.

For example:

var Cat = Backbone.Model.extend({
  defaults: {
    name: '',
    age: null
  },

  idAttribute: 'name'
});

var PushCollection = Backbone.Collection.extend({
  initialize: function () {
    coll = this;
    somePushConnection.on('deleted', function (deleted) {
      _.each(deleted, function (obj) {
        // obj being something like: {name: 'mittens', age: 302}
        var model = coll.get(obj[coll.model.idAttribute]); // Can't do this!
        if (model) { model.destroy(); }
      });
    });
  }
});

var Cats = PushCollection.extend({
  model: Cat
});
  • 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-16T00:42:04+00:00Added an answer on June 16, 2026 at 12:42 am

    You should be able to access it via the model’s prototype:

    Model.prototype.idAttribute
    

    Or in your sample code

    var model = coll.get(obj[coll.model.prototype.idAttribute]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tested this Curl code to download multiple pages simultaneously. But I want to
I have a list box with a collection as its data context and I
Getting this error when try to add an item to my repositories/context: Collection has
Context (doc count) ------------------------- clojure.core/count ([coll]) Returns the number of items in the collection.
I am currently setting the connection string for my linq to sql data context
Context Sometimes, I like reading code on paper rather than on screen (so I
I want to test some new functionality which is part of an internal web
I have the following code: Hashtable env1 = new Hashtable(); env1.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,com.ibm.websphere.naming.WsnInitialContextFactory); log.info(Executed step 1);
I'm trying to push a subset of a collection of data through WCF to
I want to loop through a collection of objects and add them all to

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.