Mark O Keeffe
Mark O Keeffe

Reputation: 301

Fundamental issue with backbone collections

Being trying to get my head around Backbone, and was working away fine until I hit the following snag which makes me think I'm going about this wrong.

In the following code, I have a model and a collection. The collection is called bb.model.Settings an a function called changetheme which gets a value. Now I have the value getting this far, but when I go to save this item, Do I need to pass it to the model. I'm trying to call the save function in the model but am wondering do I actually need to this, it keeps failing anyway. Should I just save the collection or what is best way to save this

bb.model.Setting = Backbone.Model.extend(_.extend({    
    defaults: {
        theme: 'e'
    },

    initialize: function() {
        var self = this
        _.bindAll(self)
    },
    save: function() {
        var self = this
        _.bindAll(self)
    },
}))


bb.model.Settings = Backbone.Collection.extend(_.extend({  
    model:  bb.model.Setting,
    localStorage: new Store("settingb"),

    initialize: function() {
        var self = this
        _.bindAll(self)
    },
    changetheme: function(value) {
        var self = this
        _.bindAll(self)
        this.remove
        this.model.save() 
    },
}))

Upvotes: 2

Views: 239

Answers (1)

Sneaky Wombat
Sneaky Wombat

Reputation: 1848

Try checking out this fiddle, you had a couple of errors in your code:

http://jsfiddle.net/8gDqb/1/

Here's the js portion, see the fiddle for full working example:

var bb = {};
bb.model={};

bb.model.Setting = Backbone.Model.extend({    
    defaults: {
        theme: 'e'
    },

    initialize: function() {
        var self = this
        //_.bindAll(self) // no longer necessary with backbone.js
    },
    save: function() {
        var self = this
        //_.bindAll(self) // no longer necessary with backbone.js
    },
});


bb.model.Settings = Backbone.Collection.extend({  
    model:  bb.model.Setting,
    //localStorage: new Store("settingb"),

    initialize: function() {
        var self = this
        //_.bindAll(self) // no longer necessary with backbone.js
    },
    changetheme: function(value) {
        var self = this
        _.bindAll(self)
        //this.remove
        //_.bindAll(self) // no longer necessary with backbone.js
    },
});

Also, just in case you're reading old howtos about backbone in the net, you don't need to bindAll anymore. That hasn't been necessary for quite some time.

Upvotes: 2

Related Questions