-2

Hello im trying to save a model to database. I'm just typing in the value of a title to save it because my id is auto increment. I have tried it but it didn't worked. Can anyone help me?

And what urlRoot or url function i need to specify in my Backbone Model? Do I need to specify url of my collection or?

Model:

var DocumentUser = Backbone.Model.extend({
    urlRoot: '/app_dev.php/user'
});

Here is my save function:

save: function(method, model, options) {
    this.model = new DocumentUser();
    this.model.save({
        title: $('#title').val()
    }, {
        success: function(model, respose, options) {
            console.log('The model has been saved to the server');
        },
        error: function(model, xhr, options) {
            console.log('Something went wrong while saving the model');
        }
    });
}
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
neko
  • 5
  • 2

1 Answers1

0

You can use the Backbone model.save([attributes], [options]) to save a model to your database (or alternative persistence layer), by delegating to Backbone.sync.

If the model isNew, the save will be a "create" (HTTP POST), if the model already exists on the server, the save will be an "update" (HTTP PUT).

Code:

var DocumentUser = Backbone.Model.extend({
        default: {
            title: ''
        },
        urlRoot: '/app_dev.php/user'
    }),
    documentUser = new DocumentUser();

documentUser.save({
    title: $('#title').val()
}, {
    success: function(response) {
        console.log('The model has been saved to the server', response);
    },
    error: function(response) {
        console.log('Something went wrong while saving the model', response);
    }
});
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • Thanks for that, but what url i need to put in model? Url of my collection with json response of all the models or? – neko Jun 29 '16 at 07:54