underscore666
underscore666

Reputation: 1739

How to use backbone in this context

I have this piece of code:

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('#result').html(data);
    alert('Load was performed.');
  }
});

where

data = "<p>Hello</p>"

Now let's suppose I would use to change this piece of code for use backbone and template

I will create a file myTemplate.html

<p>{{data}}</p>

and the response of the server will be {data: "Hello"} instead of <p>Hello</p>

Let's suppose I have also created my view/model and collection (MyView, MyData, MyCollection) using Backbone.

How should I modify my piece of ajax code?

Upvotes: 1

Views: 139

Answers (1)

antonjs
antonjs

Reputation: 14318

I suppose you should modify your ajax success handler in this way:

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {

    // if data is not a collection 
    var myData = new MyData();
    myData.set(data);

    // if data is a collection 
    var myCollection = new MyCollection();
    myCollection.add(data);

    alert('Load was performed.');
  }
});

Upvotes: 1

Related Questions