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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:19:25+00:00 2026-06-15T02:19:25+00:00

Is this really the best way to add an item to an array in

  • 0

Is this really the best way to add an item to an array in a Backbone Model?

// TODO: is there a better syntax for this?
this.set(
    'tags',
    this.get('tags').push('newTag')
)
  • 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-15T02:19:27+00:00Added an answer on June 15, 2026 at 2:19 am

    You can implement model.push like this:

    var model, Model;
    
    Model = Backbone.Model.extend({
      defaults: { tags: [] },
      push: function(arg, val) {
        var arr = _.clone(this.get(arg));
        arr.push(val);
        this.set(arg, arr);
      }
    });
    model = new Model;
    model.on("change:tags", function(model, newTags) {
      console.log(newTags)
    });
    model.push("tags", "New tag1")
    model.push("tags", "New tag2")
    

    But maybe you should store tags in Collection, listen to its events and update models tags attribute.

    var model, Model, Tags, Tag;
    
    // Override id attribute for Tag model
    Tag = Backbone.Model.extend({
      idAttribute: "name"
    });
    
    Tags = Backbone.Collection.extend({model: Tag});
    
    Model = Backbone.Model.extend({
      initialize: function() {
        this.tags = new Tags;
        this.tags.on("add remove reset", this.updateTags, this);
      },
      updateTags: function() {
        this.set("tags", this.tags.pluck("name"))
      }
    });
    
    model = new Model;
    model.on("change:tags", function(model, newTags) {
      console.log(newTags)
    });
    
    // Reset tags
    model.tags.reset([{name: "New tag1"}, {name: "New tag2"}]);
    
    // Add tags
    model.tags.add({name: "New tag3"});
    
    // Remove tag
    model.tags.remove(model.tags.get("New tag3"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a really common use case but I've never known the best way
This is a question about the best way to add up a series of
This is really just a best practices question... I find that When developing an
I don't know whether this is really possible, but I'm trying my best. If
I really can't work out how to best do this, I can do fairly
I'm not really sure how to ask this question, but I'll do my best.
Should have asked someone this a long time ago. What is the best way
Possible Duplicate: What is the best way to get and set a single cookie
I know it's not really the best way to do it, but I have
I'm trying to come up with the best way to add the total time

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.