为此,我使用默认的 Zepto 1.0 和 typeahead.js 0.9.3(这是我撰写本文时的当前发布版本)
抛出的第一个错误实际上不是您将遇到的最后一个问题,但这是我为使其正常工作所做的(以及我添加的 CSS 以使其看起来属于 Foundation 4)。首先添加缺少的延迟支持,我包括simply-deferred。一旦 Zepto.js 和 simple-deferred 被加载,你需要添加这一行来使 simple-deferred 的行为类似于 jQuery:
Deferred.installInto(Zepto);
然后您需要对 typahead.js 进行 2 处更改,第一个 (line 328),获取远程数据的原始函数使用 jQuery 模式 .$(ajax(url, {options}),如下所示:
var that = this, jqXhr = pendingRequests[url];
if (!jqXhr) {
incrementPendingRequests();
jqXhr = pendingRequests[url] = $.ajax(url, this.ajaxSettings).always(always);
}
为了完成这项工作,Zepto(谁的$.ajax() 函数只接受一个参数,并将 URL 作为{options} 对象的一部分)将代码更改为如下所示:
var that = this, jqXhr = pendingRequests[url], ajaxOptions = {'url': url};
if (!jqXhr) {
incrementPendingRequests();
$.extend(ajaxOptions, this.ajaxSettings);
jqXhr = pendingRequests[url] = $.ajax(ajaxOptions).always(always);
}
这种方法适用于 Zepto 和 jQuery。然后在 typaeahead.js 的底部有两个对 jQuery 的硬编码引用,如下所示:
jQuery.fn.typeahead = function(method) {
if (methods[method]) {
return methods[method].apply(this, [].slice.call(arguments, 1));
} else {
return methods.initialize.apply(this, arguments);
}
};
})();
})(window.jQuery);
这很容易改成更兼容的:
$.extend($.fn, {
typeahead: function(method) {
if (methods[method]) {
return methods[method].apply(this, [].slice.call(arguments, 1));
} else {
return methods.initialize.apply(this, arguments);
}
}
});
})();
})(window.$);
最后,因为 typeahead.js 在初始化时向 DOM 添加了一些标记,这与一些基础.css 混淆,以应对它(并设置它创建的 HTML 的样式,使其看起来更适合基金会)我添加了这个 CSS:
span.tt-dropdown-menu {
background-color: #fff;
border: 1px solid #ccc;
margin-top: -15px;
width: 100%;
}
div.tt-suggestion.tt-is-under-cursor {
background-color: #ccc;
}
div.tt-suggestion > p {
margin: 0;
line-height: 35px;
}
span.twitter-typeahead {
width: 100%;
}
我希望这可以节省其他人我整理的时间:)