简体   繁体   中英

Ember.js - call methods in views initialized within handlebars

I have an App.WidgetView which is responsible for setting up and rendering a pretty expensive 3rd party ui element, lets call this ui element widget . This widget has a refresh() method. I defined a refreshWidget() method in App.WidgetView which calls widget.refresh() .

App.WidgetView = Em.View.extend({
    content: null,
    widget: null,
    init: function () {
        this._super();
        //initialize widget using content
    },
    refreshWidget: function () {
        this.get('widget').refresh();
    }
}); 

I have a list of WidgetView s rendered on my page using an #each loop pulling data from App.widgetController

{{#each App.widgetController}}
    {{view App.WidgetView contentBinding="this"}}
{{/each}}

Now I need to have a refreshAll() method in App.widgetController and im not too shure how to do this cleanly. Right now I have a refresher property in App.widgetController which starts at 0 and gets incrimented every time refreshAll is called.

App.widgetController.reopen({
    refresher: 0,
    refreshAll: function () {
        this.incrementProperty('refresher');
    }
});

Then in App.WidgetView there is a refresherBinding which is bound to App.widgetController.refresher and an observer which calls refreshWidget when refresher changes

App.WidgetView.reopenClass({        
    refresherBinding: 'App.widgetController.refresher',
    refresherChanged: function () {
        this.refreshWidget();
    }.observes('refresher')
});

It does work, but im wondering if there's a clearner way to do it.

听起来您应该有一个负责实现refreshAll的父视图,然后可以遍历它的childViews告诉它们刷新。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM