簡體   English   中英

在ember.js的控制器中使用其他路由模型

[英]using another routes model in the controller in ember.js

我在為單獨的路線在控制器中正確訪問模型時遇到一些麻煩。

目前我正在繼續...

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()數量屬性。

同樣,就條紋而言,一切工作都很好,我可以成功付款並完成該聲明。

您剛剛超出范圍,請this modelmodel進行引用,並在完成后使用它。

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);
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM