如何通过我的模块化 js 结构中的 ajax 调用接收回数据?
How to receive back the data via ajax call in my modular js structure?
这是一个非常基本的 java-脚本,具有模块化结构,基本上我想做的是,通过 API 请求随机报价,将它们打印在 HTML 页来自 Mustache.js。之前没有使用模块化结构的方式,我设法完成了这个任务,但我也想尝试模块化的方式。
我现在面临的问题是,每当我尝试呈现我的数据(即引述 + 作者)时,我都会在控制台上收到一个错误,指出函数未定义。
请检查我的代码~
(function (){
var quoting ={
quotei : [],
template : $("#quoteTemplate").html(),
init: function (){
this.cacheDom();
this.bindEvents();
this.createQuote();
this.recieve();
this.renderx();
},
cacheDom: function(){
this.$el = $('#quotez');
this.$button = this.$el.find('button');
this.$template = this.$el.find('#quoteTemplate').html();
},
bindEvents: function(){
this.$button.on('click',this.createQuote.bind(this));
},
renderx: function(data){
this.$el.html(Mustache.render(this.template,data));
},
createQuote: function(){
$.ajax({
url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
type:'GET',
data:{},
dataType:'json',
success : function(data){;
this.render(data)
},
beforeSend: function(xhr){
xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
}
});
},
};
quoting.init();
})()
请帮助我并原谅任何错误,因为这是我第一次在 Whosebug 上发帖。
这里是重构后的代码
工作演示:here
输出:
[对象对象] {
作者:"Martin Luther King Jr.",
类别:"Famous",
引用:"In the End, we will remember not the words of our enemies, but the silence of our friends."
}
代码:
(function ($) {
function quoting() {
this.quotei = [];
this.template = $("#quoteTemplate").html();
this.init();
}
quoting.prototype = {
init: function () {
this.cacheDom();
this.bindEvents();
this.createQuote();
//this.recieve();
//this.renderx();
},
cacheDom: function(){
this.$el = $('#quotez');
this.$button = this.$el.find('button');
this.$template = this.$el.find('#quoteTemplate').html();
},
bindEvents: function(){
this.$button.on('click', this.createQuote.bind(this));
},
renderx: function(data) {
console.log(data);
//this.$el.html(Mustache.render(this.template,data));
},
createQuote: function(){
var self = this;
$.ajax({
url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
type: 'GET',
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
}
}).done(function(data) {
self.renderx(data);
})
}
};
var myQuote = new quoting();
})(window.jQuery);
这是一个非常基本的 java-脚本,具有模块化结构,基本上我想做的是,通过 API 请求随机报价,将它们打印在 HTML 页来自 Mustache.js。之前没有使用模块化结构的方式,我设法完成了这个任务,但我也想尝试模块化的方式。
我现在面临的问题是,每当我尝试呈现我的数据(即引述 + 作者)时,我都会在控制台上收到一个错误,指出函数未定义。
请检查我的代码~
(function (){
var quoting ={
quotei : [],
template : $("#quoteTemplate").html(),
init: function (){
this.cacheDom();
this.bindEvents();
this.createQuote();
this.recieve();
this.renderx();
},
cacheDom: function(){
this.$el = $('#quotez');
this.$button = this.$el.find('button');
this.$template = this.$el.find('#quoteTemplate').html();
},
bindEvents: function(){
this.$button.on('click',this.createQuote.bind(this));
},
renderx: function(data){
this.$el.html(Mustache.render(this.template,data));
},
createQuote: function(){
$.ajax({
url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
type:'GET',
data:{},
dataType:'json',
success : function(data){;
this.render(data)
},
beforeSend: function(xhr){
xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
}
});
},
};
quoting.init();
})()
请帮助我并原谅任何错误,因为这是我第一次在 Whosebug 上发帖。
这里是重构后的代码
工作演示:here 输出: [对象对象] { 作者:"Martin Luther King Jr.", 类别:"Famous", 引用:"In the End, we will remember not the words of our enemies, but the silence of our friends." }
代码:
(function ($) {
function quoting() {
this.quotei = [];
this.template = $("#quoteTemplate").html();
this.init();
}
quoting.prototype = {
init: function () {
this.cacheDom();
this.bindEvents();
this.createQuote();
//this.recieve();
//this.renderx();
},
cacheDom: function(){
this.$el = $('#quotez');
this.$button = this.$el.find('button');
this.$template = this.$el.find('#quoteTemplate').html();
},
bindEvents: function(){
this.$button.on('click', this.createQuote.bind(this));
},
renderx: function(data) {
console.log(data);
//this.$el.html(Mustache.render(this.template,data));
},
createQuote: function(){
var self = this;
$.ajax({
url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
type: 'GET',
dataType: 'json',
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
}
}).done(function(data) {
self.renderx(data);
})
}
};
var myQuote = new quoting();
})(window.jQuery);