Ember:在承诺结算后过渡到路线

时间:2014-04-19 16:50:03

标签: ember.js

我有一个使用StripeCheckout的Ember控制器如下:

App.BasketController = Ember.ObjectController.extend({

    self: this,

    handler: StripeCheckout.configure({
        key: 'key',
        image: 'img/logo.png',
        token: function (token, args) {
            // Use the token to create the charge with a server-side script.
            // You can access the token ID with `token.id`

            $.post('api/payment', {token: token, args: args}, self.didPurchase.bind(self));
        }
    }),

    didPurchase: function (response) {

        window.console.log('ajax success', response);

        App.order = new App.Order({
            reference: response.reference,
            items: App.userBasket.get('items'),
            cost: App.userBasket.get('cost')
        });

        this.transitionToRoute('order-complete');
    }
});

我已经尝试了许多方法来使购买功能正常工作,但它无法设置transitionToRoute,因为this永远不是控制器,即使我绑定,$ .proxy等。

1 个答案:

答案 0 :(得分:3)

您使用self所做的事情无法奏效。您需要有效地捕获对控制器的引用。这样的事情应该有效:

App.BasketController = Ember.ObjectController.extend({
  handler: null, // will be set as handler for StripeCheckout
  initializeStripeHandler: function(){
    var self = this;
    this.handler = StripeCheckout.configure({
      key: 'key',
      image: 'img/logo.png',
      token: function (token, args) {
        // Use the token to create the charge with a server-side script.
        // You can access the token ID with `token.id`
        $.post('api/payment', {token: token, args: args}, self.didPurchase.bind(self)));
      }
    }),
  }.on('init')
  didPurchase: function (response) {
      window.console.log('ajax success', response);
      App.order = new App.Order({
          reference: response.reference,
          items: App.userBasket.get('items'),
          cost: App.userBasket.get('cost')
      });
      this.transitionToRoute('order-complete');
  }
});