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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:56:28+00:00 2026-06-15T10:56:28+00:00

I have this model defaults: test.Models.ItemModel = Backbone.Model.extend({ defaults: { name: ‘an item’, units:

  • 0

I have this model defaults:

test.Models.ItemModel = Backbone.Model.extend({

defaults: {
    name: 'an item',
    units: []
},

Which I then use the following code to set the Model:

 addUnit: function(e){
    if(e.keyCode == 13){
      this.model.set({ 'units' : this.model.get('units').push($('#addUnit').val()) }, 
        {success: function(){
          this.render();
        }}
      );
    }
  },

However, it never seems to get added to the Model array, am I doing things right here??

  • 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-15T10:56:29+00:00Added an answer on June 15, 2026 at 10:56 am

    The problem is that you’re assuming the the push method is returning the whole array; instead, as stated here, the push method

    Returns the new length property of the object upon which the method was called.

    So, you need to push the element into the array before you set it to the model :

    var _units = this.model.get('units');
    _units.push($('#addUnit').val());
    this.model.set({ 'units' : _units });  
    

    Careful though, this will modify anything else that points to this array, so if you do this, for example:

    var myArray = [1,2,3]
    this.model.set({units: myArray})
    var _units = this.model.get('units')
    _units.push(4)
    this.model.set({ 'units' : _units })
    myArray == this.model.get('units') // holy moly, they're the same :(
    

    If you want to avoid this, or still want to use a single line of code for that, you could use array’s concat method:

    this.model.set({ 
        'units' : this.model.get('units').concat($('#addUnit').val())
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have this model: var myParentModel = Backbone.Model.extend({ defaults:{ parent1: null, parent2: null}
I have this model var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess
Here is what I have so far: var Item = Backbone.Model.extend({ defaults: { id:
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
Assume I have this model : class Task(models.Model): title = models.CharField() Now I would
I have a model which has a nullable foreign key relation with itself (this
I have the following models: # Group for Key/Value pairs class Group(models.Model): name =
I have this model: var HuntGroupSchema = new Schema({ name : { type: String,
If have the following Django (1.4) model: from django.db import models class SimpleModel(models.Model): name
I have a model and a manager as class PlaylistManager(models.Manager): def add_playlist(self, name): playlist

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.