Can I bind form inputs to models in Backbone.js without manually tracking blur events?

后端 未结 6 1488
抹茶落季
抹茶落季 2021-01-29 18:32

I have a backbone.js app (www.github.com/juggy/job-board) where I want to bind my form inputs directly to my model (a la Sproutcore).

Is it possible with Backbone.js (or

6条回答
  •  无人共我
    2021-01-29 18:48

    I'm not sure how SC does it but probably they listen for events too.

    window.SomeView = Backbone.View.extend({
      events: {
        "change input.content":  "contentChanged"
      },
      initialize: function() {
        _.bindAll(this, 'contentChanged');
        this.inputContent = this.$('input.content');
      },
      contentChanged: function(e) {
        var input = this.inputContent;
    
        // if you use local storage save 
        this.model.save({content: input.val()});
    
        // if you send request to server is prob. good idea to set the var and save at the end, in a blur event or in some sync. maintenance timer.
        // this.model.set({content: input.val()});
      }
    });
    

提交回复
热议问题