0

I have a very simple model called "gameModel" and a collection "gamesCollection" shown below. When I try to create a new game in the collection it sends a post to the server but that post has no data in it. Does any know what is going on?

//The relevant code
var game = new gameModel();
game.gameId = id;
gamesCollection.create(game);

...........
//The Collection 
define([
  'jQuery',
  'Underscore',
  'Backbone',
  'collections/common/mixin',
  'models/game',
  'config/restresource'
    ], function($, _, Backbone, collectionMixin, gameModel, restDomain){
    var gamesCollection = Backbone.Collection.extend({
        url: (new restDomain()).getGamesRoot(),
        model: gameModel,
        initialize: function(){
            _.extend(this.__proto__, collectionMixin);
        },

        parse: function(resp, xhr) {
            var games = [];
            for(i=0;i<resp.length;i++){
                games.push(new gameModel({gameId: resp[i].gameId}));
            }
            return games;
        },
    });

    return new gamesCollection;
});
Usman Ismail
  • 17,999
  • 14
  • 83
  • 165

1 Answers1

1

Try this

gamesCollection.create({"id":id});

Backbone.Collection.create accepts attributes as the first argument, not a model... If a model is successfully created, returned is a model. So, you may want to implement it like the following if you still wish to use the game model object.

var game = gamesCollection.create({"id":id});
rudolph9
  • 8,021
  • 9
  • 50
  • 80
  • Why not `gamesCollection.create(game.toJSON())`? – Lukas Jan 14 '13 at 22:05
  • 2
    @Lukas because then you have two model objects with the same data... if you need to _add_ an existing model to a collection then use [`Backbone.Collection.add`](http://backbonejs.org/#Collection-add). – rudolph9 Jan 14 '13 at 22:07
  • My problem was that I thought the intent of create was to "create" a model client side and save it to the server. – Usman Ismail Jan 15 '13 at 13:44