我试图将数据添加到动态模态。一旦数据被添加到模型中,模态应该只能被看到。我试图使用$.when(Ss.pieceInfo(piece)).then(Ss.showInfo());
行来实现这一目标,但似乎两个函数都在同一时间执行。什么在AJAX完成对象文字后执行函数
var Ss = {
$modal: $('#piece-modal'),
$title: $('.piece-title'),
$artist: $('.piece-artist'),
$info: $('.piece-info'),
init: function() {
this.bindEvents();
},
bindEvents: function() {
$('.piece').on("click", function(e) {
e.preventDefault();
var piece = $(this).data('slug');
//This doesn't seem to have any effect
$.when(Ss.pieceInfo(piece)).then(Ss.showInfo());
});
},
pieceInfo: function(piece) {
console.log(piece);
$.getJSON(Kirby.baseUrl + '/api/v1/work/' + piece, function(data) {
$.each(data, function(key, val) {
Ss.$title.html(val.title);
Ss.$artist.html(val.artist);
Ss.$info.html(val.text);
})
});
},
showInfo: function() {
var height = $('.primary-info').height();
console.log(height)
Ss.$modal.addClass('modal-active')
Ss.$modal.css('transform','translate3D(0, calc(100% - '+ height +'px), 0)');
},
};
$(document).ready(function(){
Ss.init();
});
尝试在您的方法中添加一个稍微超时以更新模态。 这样你就可以看到它们是否在同一时间实际执行。 – inoabrian