【问题标题】:using another routes model in the controller in ember.js在 ember.js 的控制器中使用另一个路由模型
【发布时间】:2014-03-11 02:01:34
【问题描述】:

我在控制器中为单独的路由正确访问模型时遇到了一些问题。

目前我正在做这件事......

App.CheckoutRoute = Ember.Route.extend({
model: function(){
    return this.modelFor('product');
}
});

这在我的模板中有效,它似乎在控制器的其他属性中

App.CheckoutController = Ember.ObjectController.extend({
publishable: 'pk_test_AtBneKs2kGmWkyD60ymyh5fw',

number: '',
cvc: '',
expMonth: '',
expYear: '',

errors: '',

charge: function() {
    var p = this.get('model.price');

    return p + '00';
}.property('model.price'),

actions: {
    tokenize: function() {
        //disable the submit button to prevent repeated clicks
                 $('button[type=submit]').attr("disabled", "disabled");

                 //Set Stripe Publishable Key
                 Stripe.setPublishableKey(this.get('publishable'));

                 // createToken returns immediately - the supplied callback submits the form if there are no errors
                 Stripe.createToken({
                          number: this.get('number'),
                          cvc: this.get('cvc'),
                          exp_month: this.get('expMonth'),
                          exp_year: this.get('expYear')
                      }, this.didCreateToken.bind(this));

                 return false;
    }
},

didCreateToken: function(status, response) {
    // console.log(status);
    // console.log(response);
    if(response.error) {
        $('button[type=submit]').removeAttr('disabled');
        return this.set('errors', response.error.message);
    }else{
        var form = $("#payment-form");
        // token contains id, last4, and card type
        var token = response['id'];
        // insert the token into the form so it gets submitted to the server
        form.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
        // post via ajax
        $.ajax({
            url: 'stripe/submit.php',
            type: 'post',
            data: $('#payment-form').serialize()
        })
        .done(function(data, status, xhr) {
            console.log(data);
            console.log(status);
            console.log(xhr);
        })
        .fail(function(data, status, xhr){
            console.log(data);
            console.log(status);
            console.log(xhr);
        });
    }
}
});

当我尝试访问模型以更新其数量属性以持久保存到我的解析服务器时,问题就出现了。

我想在 didCreateToken 函数的 done 语句中执行此操作,但尝试像平常一样获取模型我在控制台中收到一个错误,说它没有方法 get。我如何才能访问模型,以便能够在条带付款结束后更新和 .save() 数量属性。

就条纹而言,一切工作正常,我可以成功付款并获得完成声明。

【问题讨论】:

    标签: javascript ember.js model controller ember-data


    【解决方案1】:

    您刚刚超出范围,设置对thismodel 的引用并在完成后使用它。

    didCreateToken: function(status, response) {
        var self = this;
        // console.log(status);
        // console.log(response);
        if(response.error) {
            $('button[type=submit]').removeAttr('disabled');
            return this.set('errors', response.error.message);
        }else{
            var form = $("#payment-form");
            // token contains id, last4, and card type
            var token = response['id'];
            // insert the token into the form so it gets submitted to the server
            form.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
            // post via ajax
            $.ajax({
                url: 'stripe/submit.php',
                type: 'post',
                data: $('#payment-form').serialize()
            })
            .done(function(data, status, xhr) {
                var model = self.get('model');
                console.log(data);
                console.log(status);
                console.log(xhr);
            })
            .fail(function(data, status, xhr){
                console.log(data);
                console.log(status);
                console.log(xhr);
            });
        }
    }
    

    【讨论】:

    • 所以当你在这样的承诺中时你会超出范围?
    • this 在异步回调函数(传递给 done 的函数)内时更改范围。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多