xorinzor
xorinzor

Reputation: 6467

Javascript store backbone model instance in object

I am trying to store created instances in an object, unfortunatly it keeps producing errors like Uncaught SyntaxError: Unexpected token ,

This is an example of my Backbone model code:

Hero = Backbone.Model.extend({
    defaults: {
        id:         0,
        name:       ''
    },

    initialize: function(){
    }
});

and this is the way I try to create and store the instances

var heroes = {
    new Hero({ 0, 'Orrin'}),
    new Hero({ 1, 'Valeska'})
}

How am I supposed to store these instances?

Upvotes: 0

Views: 263

Answers (2)

zhujy_8833
zhujy_8833

Reputation: 571

did you notice that defaults: { id: 0, name: '', }, there is a ',' at the end of 'name' attribute? Try to delete it and see if that would fix the problem.

Upvotes: 0

machineghost
machineghost

Reputation: 35750

new Hero({ 0, 'Orrin'}),
new Hero({ 1, 'Valeska'})

That's not valid Javascript. You sort of combined an array and an object; you should either do:

new Hero([ 0, 'Orrin']),
new Hero([ 1, 'Valeska'])

(which is valid Javascript, but not valid Backbone) ... or, more likely:

new Hero({index: 0, name: 'Orrin'}),
new Hero({index: 1, name: 'Valeska'})

Upvotes: 1

Related Questions