0

I have an app in which I am not using Backbone Views, only Backbone Models.

The structure is the one that follows.

#html
<tr>
  <td >Personal</td>
  <td id="personal-data" data-content="<?=str_replace('"',"'",json_encode($data))?>">
     <input type="text" name="name" placeholder="Name"><br>
     <input type="text" name="address" placeholder="Address">
  </td>
<tr>
#PersonalModel.js
var Personal = Backbone.Model.extend({
   urlRoot: "/api/personal"
});
$(document).ready(function () {
  var personal = new Personal($('td#personal-data').data('content'));
  $('td#data-ept *').on('change', function () {
    personal.set(this.name, this.value);
    personal.save()

  });
});

this.name is always equal to one of the model's attributes being persisted or updated.

Jorge
  • 333
  • 1
  • 5
  • 17
  • Side note: Don't do `str_replace('"',"'",json_encode($data)`, that's unclean. What you actually want here is *"make a string safe for use in HTML"*, and the appropriate function for that is `htmlspecialchars(json_encode($data))`. – Tomalak Jan 15 '19 at 13:52

1 Answers1

0

The problem was that I was working also with a plugin that was validating the form. So every time I changed an input value there was a div that was changing. I solved the problem with this code

$(document).ready(function () {
  var personal = new Personal($('td#personal-data').data('content'));
  $('td#data-ept *[name]').on('change', function () {
    personal.set(this.name, this.value);
    personal.save()
  });
});
Jorge
  • 333
  • 1
  • 5
  • 17