Ember.js sendAction()方法。访问控制器中的操作

Ember.js sendAction()方法。访问控制器中的操作,ember.js,Ember.js,我需要访问在HomeController中的操作中定义的方法,但我一直得到一个未定义的方法。 我发现有一个sendAction()方法就是为这个而设计的,但是在阅读了文档、github问题和一些(只有少数)与stackoverflow相关的主题之后,我放弃了,决定采用一种肮脏的方式。 但是,我仍然明白,这是正确的使用方法。 代码如下: ArtRank.HomeController = Ember.ObjectController.extend({ photoIndex: 0,

我需要访问在HomeController中的操作中定义的方法,但我一直得到一个未定义的方法。 我发现有一个sendAction()方法就是为这个而设计的,但是在阅读了文档、github问题和一些(只有少数)与stackoverflow相关的主题之后,我放弃了,决定采用一种肮脏的方式。 但是,我仍然明白,这是正确的使用方法。 代码如下:

    ArtRank.HomeController = Ember.ObjectController.extend({
    photoIndex: 0,

    actions: {
        nextPhoto: function() {    
            this.set('photoIndex', this.get('photoIndex') + 1);
            var items;

        },
        prevPhoto: function() {
            this.set('photoIndex', this.get('photoIndex') - 1);
        }
    },

    init: function() {
        var controller = this;
        this.set('photoTimer', setInterval(function(){
            Ember.run(function() {
                controller.nextPhoto();
            });
        }, 3000));
    }

});

我需要访问init函数中的nextPhoto()。但是nextPhoto()在actions中,所以它给了我一个未定义的

只需使用controller.send(actionName,args…)

观察:我不知道是否可以在
init
方法内部触发操作,因此我使用了
startTimer:function(){}。在('init')
上,此方法将在对象创建和设置之后触发

ArtRank.HomeController = Ember.ObjectController.extend({
    photoIndex: 0,

    actions: {
        nextPhoto: function() {    
            this.set('photoIndex', this.get('photoIndex') + 1);
            var items;

        },
        prevPhoto: function() {
            this.set('photoIndex', this.get('photoIndex') - 1);
        }
    },

    startTimer: function() {
        var controller = this;
        this.set('photoTimer', setInterval(function(){
            Ember.run(function() {
                controller.send('nextPhoto');
            });
        }, 3000));
    }.on('init')
});

很酷,谢谢,很有效!所以我做对了,但在init中不起作用,很有趣。