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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:11:08+00:00 2026-06-07T19:11:08+00:00

I would like to make sure that a specific attribute is unique across the

  • 0

I would like to make sure that a specific attribute is unique across the collection when a new model is created so that I can ultimately use this attribute as a permalink.

I have a List Collection and List Model. (CoffeeScript)

class App.Models.List extends Backbone.Model
  defaults:
    name: "My New List"

class App.Collections.ListSet extends Backbone.Model
  model: App.Models.List

When a new model is added to the ListSet, I would like to add a auto-incrementing number to the end of the name attribute if a model exists with that name already. For example My New List 1 if a model existed with My New List.

I don’t want it to be obtrusive and show an error, but just fix the problem automatically. I am happy with the logic behind adding the incrementing integer, but I need direction on where to put the code to make the changes.

  • 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-07T19:11:10+00:00Added an answer on June 7, 2026 at 7:11 pm

    This might work for you:

    App.Models.List = Backbone.Model.extend({
        initialize: function(attrs) {
            this.set(attrs);
            this.set({originalName: attrs.name});
    
            var count = yourCollection.reduce(function(count,model){
                return count + (model.get('originalName') === this.get('originalName') ? 1 : 0);
            },0,this);
    
            if(count > 0) {
                this.set({name: this.get('name') + ' ' + count});
            }
        }
    });
    

    Here’s a demo: jsFiddle DEMO

    What this should do is every time you initialize a model, you scan your collection to see if any other models have that same originalName. If so, you tack on the result to the name attribute.

    === UPDATE ===

    I realize now that it’s probably better to do the modification when the model is added to the collection, so as to decouple the model from the collection. Here’s an updated code snippet:

    App.Models.List = Backbone.Model.extend({
        initialize: function(attrs) {
            this.set({originalName: attrs.name});
        }
    });
    
    App.Collections.Lists = Backbone.Collection.extend({
        model: App.Models.List,
        initialize: function() {
            this.on('add',this.resolveNameConflict,this);
        },
        resolveNameConflict: function(model){
            var count = this.reduce(function(c,m){
                return c + (m.get('originalName') === model.get('originalName') ? 1 : 0);
            },0);
            if(count > 1) {
                model.set({name: model.get('originalName') + ' ' + (count-1)});
            }
        }
    });
    

    and a demo: jsFiddle DEMO

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

Sidebar

Related Questions

I would like to make sure that an applescript can be converted to bash.
My co-workers would like to make sure that our work in R is platform-independent,
I would like to make sure my classes are robust - even if they
I would like to make sure if a textbox has any content at all,
In my stored procedure, I would like to check to make sure what I
Would like to make anapplication in Java that will not automatically parse parameters used
How can I make sure that a certain instance of a class will never
I would like to make sure an advice is executed every time an exception
I would like to know how can I force a model loading in development
I would like make a script using PHP (probably need JS) to send POST

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.