AnApprentice
AnApprentice

Reputation: 110960

Backbone.js is not posting to the server on a model change? Why?

I have the following:

// MODEL
NotificationModel = C.BB.Model.extend({
    defaults : {
        read : false
    },
    urlRoot : '/notifications'
});


// COLLECTION
NotificationCollection = C.BB.Collection.extend({
    model: NotificationModel,
    url: '/notifications',
    initialize : function() {
        var me = this;
        me.fetch();
    }
});

Later in the view I have:

....

onCollectionAdd : function(m,c,cfg) {
    m.set({read: true},{silent: false});

The set is changing the items value but Backbone is not posting the update to the server. Any suggestions? Thanks

Upvotes: 1

Views: 161

Answers (2)

Andreas Köberle
Andreas Köberle

Reputation: 110922

You could bind an event to the collection to save them:

NotificationCollection = C.BB.Collection.extend({
    model: NotificationModel,
    url: '/notifications',
    initialize : function() {
        this.fetch();
        this.bind('change', this.save, this);
    }
});

Note there is also the Collection.create method wich add a new model to the collection and save the collection on the server.

Upvotes: 1

Pavel B
Pavel B

Reputation: 36

You need to call m.save(), which will call correspondent method create() or update().

Method set() trigger only change() event, which not save any data in backend

Upvotes: 2

Related Questions