我在控制器中正确访问模型时遇到了一些问题,无法使用单独的路径。
目前我还有这个......
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语句中这样做,但是试图像正常一样得到模型我在控制台中得到一个错误,说它没有方法获取。如何在条带付款完成后获取模型以便能够更新和.save()数量属性。
此外,条纹的所有内容都可以正常运行,我可以成功付款并完成完成后的声明。
答案 0 :(得分:1)
你刚刚超出范围,设置对this
或model
的引用,并在完成内部使用它。
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);
});
}
}