How to focus after initialization with emberjs?

后端 未结 2 1798
一生所求
一生所求 2021-02-20 12:26

I\'m new to Ember.js. I want to focus on TextField(in sample, id=\"text\") after initialization, but in ready function, doesn\'t work focus method...



        
相关标签:
2条回答
  • 2021-02-20 13:02

    The following code does work:

    <script type="text/x-handlebars">
          {{view App.TextField id="text"}} // want to focus it.
    </script>
    <script type="text/javascript">
      var App = Em.Application.create();
    
      App.TextField = Em.TextField.extend({
        didInsertElement: function() {
          this.$().focus();
        }
      });
    </script>
    
    0 讨论(0)
  • 2021-02-20 13:05

    Subclassing TextField and then spreading a custom View around your templates seemed a bit messy to me, so I wrote this little 1kb package that lets you do this more elegantly, directly in the template, without any further coding:

    <body>
      <!-- all the libraries -->
      <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.2.0/ember.min.js"></script>
      <script src="http://rawgithub.com/AndreasPizsa/ember-autofocus/master/dist/ember-autofocus.min.js"></script>
      <!-- your template -->
      <script type="text/x-handlebars">
        Hello, world! {{ input }}
        :
        : more elements here
        :
        {{ autofocus }} {# <<<<-- Magic happens here #}
      </script>
      <!-- your app -->
      <script>
        Ember.Application.create();
      </script>
    </body>
    

    You can get it from https://github.com/AndreasPizsa/ember-autofocus or with bower install ember-autofocus. I appreciate feedback!

    0 讨论(0)
提交回复
热议问题