Force to refresh ember view

2019-08-10 06:20发布

问题:

i have an ember view with a bunch of jquery code to manipulate the dom, the thing is when i use:

this.transitionTo("forecast.workpage", id);

it only loads the view once but when i try click it again the view doesn't execute again, i try several ways for instance adding this.rerender() in the end of my didInsertElement like that:

didInsertElement : function(){
//all my js code
this.rerender()
}

but it trows an error

Uncaught TypeError: Cannot read property '0' of undefined 

, try adding this._super() in the begin, same result, is there a way to force the view to refresh from the view itself or the route? or controller ?

edit: try this approach with no results...

didInsertElement : function(){      
        this._super();
        Ember.run.scheduleOnce('afterRender', this, this.afterRenderEvent);
    },

    afterRenderEvent: function(){

        //$(document).foundation();

        $(".sortables li:even").find('div.row').addClass("gray");


    }

Same thing, executes onces but where i click on the action doesn't execute the code.

回答1:

To re-render the whole route (page), you can use the route's refresh method. Or, as a possible solution to this.rerender() failing, you could call that after it renders the first time.

didInsertElement: function() {
    Ember.run.scheduleOnce('afterRender', this, function() {
        this.rerender();
    });
}

Personally, I wouldn't recommend either one. Your view should update automatically when the data it depends on changes. It if it's not updating, you probably have your observers set up wrong. (I don't really know your use case though, so use your best judgement.)