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

时间:2013-12-12 08:25:34

标签: javascript ember.js

假设我有一个像这样的IndexController,

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

现在我有另一个名为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');
            });
}
});

我的问题是如何在转换时调用PicCreated。我有图片列表,上传后我将返回索引页面并显示消息“Pic created”。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

transitionToRoute会返回一个承诺,因此您可以使用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();
               });
            });
        }        
    }
});