简体   繁体   English

在过渡到Ember中的另一条路线时如何调用方法?

[英]How to invoke a method upon transition to another route in Ember?

Lets say i have an IndexController like so, 可以说我有一个像这样的IndexController,

App.IndexController = Ember.ArrayController.extend({
    sortProperties: ['PicId'],
PicCreated: function () {
        alert('Pic created');
    }
});

And now i have another controller called NewPic 现在我有另一个名为NewPic的控制器

App.NewPic = Ember.Controller.extend({
    // the initial value of the `search` property
    model: this.get('model'),
    needs: ["Index"], //says we need the Index controller for this
    actions: {
  var Pic= this.store.createRecord("PicList", obj);
            PicList.save().then(function () {
               this.transitionToRoute('Index');
            });
}
});

My question is how do i invoke PicCreated upon transition. 我的问题是过渡时如何调用PicCreated。 I have list of pictures, after the upload i will go back to index page and show a message "Pic created". 我有图片列表,上传后,我将返回索引页面并显示消息“图片已创建”。

Can someone help me,please? 有人能帮助我吗?

transitionToRoute returns a promise, so you can use the then to know when the transition is finished, and call the PicCreated method: transitionToRoute返回一个promise,因此您可以使用then来知道转换何时完成,并调用PicCreated方法:

App.NewPic = Ember.Controller.extend({        
    needs: ["index"],
    actions: {
        createPic: function() {
            var newPicController = this;
            var indexController = this.get('controllers.index');
            // more code here ...

            PicList.save().then(function () {
               newPicController.transitionToRoute('index').then(function() {
                    // show th message
                    indexController.PicCreated();
               });
            });
        }        
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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