How to enable/disable save button of backbone form-view when user changes form content

痞子三分冷 提交于 2020-01-06 18:03:55

问题


I have form which gets data from backbone model. When the form is shown, they have initially value set to backbone model. Now, if any field is edited, i want to enable "save" button immediately as soon as any changes is made to field. However, if you change field value and again change it to original, it should again disable the "save" button that allows to save model.I want to achieve as one shown in this jsfiddle :http://jsfiddle.net/qaf4M/2/

I am using backbone.js and backbone.stick (http://nytimes.github.io/backbone.stickit/) to bind model to template.

I create view as follows with model as parameter

RegionManager.show(new app.myView({
  model : new app.myModel(
   {id: 1})
 }));

MY model value is something like this:

 {
   "id":1, "name:"a" , "age":21
 }

The view is as follows:

myView = Backbone.View.extend({
     template: _.template( $("#template").html() ),
      events: {
         "click #save" : "update",
     },
    bindings: {
       '#id': 'id',
       '#name': 'name',
       '#age': 'age'
    },
    initialize: function () {
   if(this.model){
       this.model.fetch();
   },
   render: function () {           
        this.$el.html( this.template );
        this.stickit(); //used library http://nytimes.github.io/backbone.stickit/
        Backbone.Validation.bind(this);
   },
   update: function() {
       this.model.save (
       {success: this.success_callback, error: this.error_callback});
   },
   success_callback: function (model, response) {
   },
   error_callback: function (model, response) {
        alert('error.');
  }
});

My template look like

 <script type="text/template" id="template">
 <form id="myForm " >
 <fieldset>
     <label>Name</label>
      <input type="text" id="name" name="name" />
      <label>Age</label>
       <input type="text" id="age" name="age" />
       <input type="hidden" id="id" name="id"/>
   </fieldset>
 <a id="save" disabled >Save Changes</a>
 </form>

I am confused where should i bind event and how or what is proper way to know the user has chagne some value and accordingly disable button when there is no cahnge in form and enable when change has been made.


回答1:


A simple solution would be to make your view listen to your model's changes:

initialize: function () {
  if(this.model){
    this.model.fetch({
      success: function(model, resp) {
        this.model._attributes = resp; // create a copy of the original attributes
        this.listenTo(this.model, 'change', function() {
          // when the model changes
          // maybe write some function to compare both
          if(_.isEqual(this.model._attributes, this.model.toJSON())) {
            // disable
          }
          else {
            // able
          }
        });
      }
    });
  }

So, when the data comes back from the server, you create a copy, and then listen to your model's changes. If the new attributes equal the original, disable the button.



来源:https://stackoverflow.com/questions/16108176/how-to-enable-disable-save-button-of-backbone-form-view-when-user-changes-form-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!