Vijaykumar K
Vijaykumar K

Reputation: 17

Send ajax request on click event in backbone js

Here's the code

This is model

 var TodoItem = Backbone.Model.extend({
     url: 'list.php',
     DeleteTabItem: function (child, parent) {
         jQuery.ajax({
             url: 'delete.php',
         });
     }
 });

This is view

var TodoView = Backbone.View.extend({
    el: '.entry-title',
    template: _.template(''),
    KeyPressEvent: function () {
        this.model.DeleteTabItem();
    }
});

Is this correct way of sending ajax request.

Thanks in Advance

Upvotes: 1

Views: 3072

Answers (2)

Roy M J
Roy M J

Reputation: 6938

In backbone model, instead of url,

use : urlRoot: "yoururl",

Backbone.Model.extend({
    urlRoot: 'list.php' 
});

url would be used in collections

For sending data through view :

this.model.save(sendData, { success, error });

where sendData = { data preferably in json }

You will have to bind the model with your view like :

var todoView = var TodoView(model:TodoItem);

Upvotes: 2

pengjunyong
pengjunyong

Reputation: 66

In Backbone world, we usually use multiple models and collection instead of handle data directly via JQuery AJAX function.

So you just need to persist your values into model or collections, and execute correspond actions, like fetch(), save(), destroy()...They have default request type.

As for your code, you still can use new function "DeleteTabItem", but inside, the better way is call some model or collection's destroy action.

Upvotes: 1

Related Questions