Coder Social home page Coder Social logo

2-in-1 for input data-bind about agility HOT 2 OPEN

arturadib avatar arturadib commented on July 24, 2024
2-in-1 for input data-bind

from agility.

Comments (2)

tristanls avatar tristanls commented on July 24, 2024

thanks @Wizek, #1 makes sense to me since that seems to be the expected HTML behavior.

As far as #2...

To me it looks like the only other types this would apply to would be input[type="text"] and textarea. I don't think it should be a default behavior because of all the events flying around as a consequence, but perhaps a toggle if one wants it. @arturadib thoughts?

My suggestion for implementation would be along the lines of adding an options data-bind-instant=true and doing something like below around https://github.com/arturadib/agility/blob/master/agility.js#L564:

// <input type="text"> and <textarea>: 2-way binding
else if ($node.is('input[type="text"], textarea')) {
  // Model --> DOM
  self.bind('_change:'+bindData.key, function(){
    $node.val(self.model.get(bindData.key)); // this won't fire a DOM 'change' event, saving us from an infinite event loop (Model <--> DOM)
  });            
  // Model <-- DOM
  if ( $node.data( 'bind-instant' ) == true ) {
    $node.keypress(function(){
      // Without timeout $node.val() misses the last entered character
      setTimeout(function(){
        var obj = {};
        obj[bindData.key] = $node.val();
        self.model.set(obj); // not silent as user might be listening to change events
      }, 50);
    });
  } // instant bind
  else { // efficient bind
    $node.change(function(){
      var obj = {};
      obj[bindData.key] = $(this).val();
      self.model.set(obj); // not silent as user might be listening to change events
    });
  }
  // 1-way attribute binding
  bindAttributesOneWay();
}

from agility.

Wizek avatar Wizek commented on July 24, 2024

@tristanls Does $node.val() also miss the last entered character if you bind for the keyup event (instead of keypress)? I hope it can be done more elegantly without setTimeout.

from agility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.