How can I bind to a view re-render in Ember.js?

后端 未结 2 949
慢半拍i
慢半拍i 2021-02-09 19:15

I have some jQuery code which needs to be run when a view is rendered. For the initial render I can easily do

App.FooView = Ember.View.extend({
  didInsertElemen         


        
相关标签:
2条回答
  • 2021-02-09 19:58

    If you would like to re-render the view when a certain attribute changes then what you want would look something like this:

    App.FooView = Ember.View.extend({
        myProperty: 'This value might change',
        myPropertyChanged: function() {
            //Do some stuff here (maybe change the template) and call...
            this.rerender();
        }.observes('myProperty')
    });
    

    But if you are looking to only re-render the view after the view is created then you want something more like this:

    App.FooView = Ember.View.extend({
        init: function() {
            this._super();
            //Do some stuff here (maybe change the template) and call...
            this.rerender();
        }
    });
    

    SOURCE: http://emberjs.com/api/classes/Ember.View.html

    0 讨论(0)
  • 2021-02-09 20:11

    In addition to Akash's explanation, you would want to use the didInsertElement hook instead of the init hook to only re-render after the view is created. So it would look more like:

    App.FooView = Ember.View.extend({
        didInsertElement: function() {
            this.rerender();
        }
    });
    

    Although, I'm not exactly sure why you'd need to rerender after the view has already rendered.

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